小能豆

TKinter:根据下拉菜单中的文件名打开 html 文件

py

我需要帮助,如何根据下拉菜单中选择的文件名打开 html 文件。

我的问题:

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

1.png

当我单击“显示数据”时,它直接转到其他浏览器。它没有将我的 html 文件显示为附加图像。
2.png

我只是想,当我在下拉列表中选择一个 html 文件然后单击“显示数据”时,它就会打开我的 html 文件。

提前致谢

这是我的代码:


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()

阅读 13

收藏
2024-12-12

共1个答案

小能豆

它只需要一个小小的改变,您需要将file://链接传递给浏览器以将其作为本地文件打开。

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

固定部分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。

2024-12-12