[英]TKinter: Open html file based on filename in dropdown menu
我需要有關如何根據下拉菜單中選擇的文件名打開 html 文件的幫助。
我的問題:
當我點擊“顯示數據”時,它 go 直接到其他瀏覽器。 它沒有將我的 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://
鏈接傳遞給瀏覽器以將其作為本地文件打開。
否則瀏覽器會自動認為文件名是服務器,因此報錯無法打開!
固定部分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。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.