TKinter:在下拉菜单中基于文件名打开html文件 [英] TKinter: Open html file based on filename in dropdown menu

查看:75
本文介绍了TKinter:在下拉菜单中基于文件名打开html文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要有关如何在下拉菜单中基于所选文件名打开html文件的帮助.

I need help on how to open html file based on selected filename in my dropdown menu.

我的问题:

当我运行我的代码时,它显示的界面如图所示.

when I run my code, it shows the interface like in image.

当我单击显示数据"时,它将直接转到另一个浏览器.它不会将我的html文件显示为附件图像.

when I clicked "show data", it go directly to the other browse. it doesn't show my html file as attached image.

我只想,当我在下拉列表中选择一个html文件然后单击显示数据"时,它将打开并打开我的html文件.

I just want, when I select a html file in dropdown list then click "Show Data", it will go and open my html file.

非常感谢

这是我的代码:

from tkinter import *
from functools import partial
import os
import tkinter as tk
from tkinter import ttk
#import tkinter as tk
from tkinter import filedialog, messagebox, ttk
import pandas as pd
import webbrowser
from webbrowser import open as openlink
import urllib

def open(file_menu):
    filename = file_menu.get()
    webbrowser.open(filename)



def clear_data():
    tv1.delete(*tv1.get_children())
    return None



folder = os.path.realpath(r'C:\Users\Downloads\testing')
filelist = [fname for fname in os.listdir(folder)]

master = tk.Tk()
master.geometry('1200x800')
master.title('THB')

# Frame for TreeView
frame0 = tk.LabelFrame(master, text="Chapter",background="light grey")
frame0.place(height=500, width=1200, rely=0.0, relx=0.0)

optmenu = ttk.Combobox(frame0, values=filelist, state='readonly')
optmenu.pack(fill='x')

button_select = tk.Button(frame0, text="Show Data",
                          width=15,
                          height=2,
                          compound=tk.CENTER,
                          command=partial(open, optmenu))

button_select.place(relx=0.5, rely=0.5)
button_select.pack(side=tk.TOP)

# Frame for TreeView
frame1 = tk.LabelFrame(master, text="Data",background="light blue")
frame1.place(height=500, width=1200, rely=0.2, relx=0.0)


## Treeview Widget
tv1 = ttk.Treeview(frame1)
tv1.place(relheight=1, relwidth=1) # set the height and width of the widget to 100% of its container (frame1).

treescrolly = tk.Scrollbar(frame1, orient="vertical", command=tv1.yview) # command means update the yaxis view of the widget
treescrollx = tk.Scrollbar(frame1, orient="horizontal", command=tv1.xview) # command means update the xaxis view of the widget
tv1.configure(xscrollcommand=treescrollx.set, yscrollcommand=treescrolly.set) # assign the scrollbars to the Treeview Widget
treescrollx.pack(side="bottom", fill="x") # make the scrollbar fill the x axis of the Treeview widget
treescrolly.pack(side="right", fill="y") # make the scrollbar fill the y axis of the Treeview widget



master.mainloop()

推荐答案

它只需要进行一点更改,就需要将 file://链接传递给浏览器以将其作为本地打开文件.

It just required a small change, you needed to pass the file:// link to the browser to open it as a local file.

否则,浏览器会自动认为该文件名是服务器,因此由于错误,无法打开该文件!

Else, the browser automatically thinks that the file name is a server, and thus for error, it can't open it!

固定部分 def open(file_menu)::

def open(file_menu):
    filename = file_menu.get()

    open_in_browser_link = f"file://{os.path.join(folder, filename)}"
    print(open_in_browser_link)

    webbrowser.open(open_in_browser_link)

完整代码:

from tkinter import *
from functools import partial
import os
import tkinter as tk
from tkinter import ttk
#import tkinter as tk
from tkinter import filedialog, messagebox, ttk
import pandas as pd
import webbrowser
from webbrowser import open as openlink
import urllib

def open(file_menu):
    filename = file_menu.get()

    open_in_browser_link = f"file://{os.path.join(folder, filename)}"
    print(open_in_browser_link)

    webbrowser.open(open_in_browser_link)



def clear_data():
    tv1.delete(*tv1.get_children())
    return None



folder = os.path.realpath(r'C:\Users\Downloads\testing')
filelist = [fname for fname in os.listdir(folder)]

master = tk.Tk()
master.geometry('1200x800')
master.title('THB')

# Frame for TreeView
frame0 = tk.LabelFrame(master, text="Chapter",background="light grey")
frame0.place(height=500, width=1200, rely=0.0, relx=0.0)

optmenu = ttk.Combobox(frame0, values=filelist, state='readonly')
optmenu.pack(fill='x')

button_select = tk.Button(frame0, text="Show Data",
                          width=15,
                          height=2,
                          compound=tk.CENTER,
                          command=partial(open, optmenu))

button_select.place(relx=0.5, rely=0.5)
button_select.pack(side=tk.TOP)

# Frame for TreeView
frame1 = tk.LabelFrame(master, text="Data",background="light blue")
frame1.place(height=500, width=1200, rely=0.2, relx=0.0)


## Treeview Widget
tv1 = ttk.Treeview(frame1)
tv1.place(relheight=1, relwidth=1) # set the height and width of the widget to 100% of its container (frame1).

treescrolly = tk.Scrollbar(frame1, orient="vertical", command=tv1.yview) # command means update the yaxis view of the widget
treescrollx = tk.Scrollbar(frame1, orient="horizontal", command=tv1.xview) # command means update the xaxis view of the widget
tv1.configure(xscrollcommand=treescrollx.set, yscrollcommand=treescrolly.set) # assign the scrollbars to the Treeview Widget
treescrollx.pack(side="bottom", fill="x") # make the scrollbar fill the x axis of the Treeview widget
treescrolly.pack(side="right", fill="y") # make the scrollbar fill the y axis of the Treeview widget



master.mainloop()

*注意:但是,请记住,如果您打开某些不受浏览器支持的文件,它们可能会要求您在计算机中使用某些其他应用程序打开.例如:如果您尝试打开pdf.

*Note: But, keep in mind, if you open some file that aren't browser supported, they may ask to open with some different application, in your computer. Example: If you try to open a pdf.

这篇关于TKinter:在下拉菜单中基于文件名打开html文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆