【发布时间】:2017-01-16 14:28:17
【问题描述】:
我创建了一些函数,可以在我用 tkinter 制作的 GUI 中显示 csv 文件的图形和表格。
我有一个menubar,带有一个 import 按钮、一个 plot 按钮和一个 table 按钮。 plot和table按钮可以分别成功绘制csv文件的图形和表格。
我想做的是,当用户选择 import 按钮时,他们会选择他们选择的文件。然后,如果他们碰巧选择了 plot 按钮,绘图功能将作用于他们从 import 中选择的文件。此外,如果他们碰巧选择了 table 按钮,则 table 函数将作用于他们从 import 中选择的文件。
我创建了一个名为openfile() 的文件打开函数,它会记住打开的文件的名称。
问题是我不知道如何使用menubar 和openfile() 这样当点击import 按钮时,我的应用程序会存储文件名。
关于我将如何进行此操作的任何提示?
这是我为menubar 和openfile() 编写的代码:
def openfile():
name= askopenfilename()
return (name[19:])
class MyApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
tk.Tk.wm_title(self, "MyApp")
# main frame
container = tk.Frame(self)
container.pack(side="top", fill="both", expand = True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
container = tk.Frame(self)
container.pack(side="top", fill="both", expand = True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
# creates the menubar at the top of the window
menubar = tk.Menu(container)
# import menu for importing csv files, initializes a file opening function (tbd)
filemenu = tk.Menu(menubar, tearoff=0)
filemenu.add_command(label="Import a CSV File", command = file_openerfunction)
menubar.add_cascade(label= "Import", menu=filemenu)
# plot menu for creating graphs and figures
Plot = tk.Menu(menubar, tearoff =0 )
Plot.add_command(label="Plot My CSV File", command= popupgraph)
menubar.add_cascade(label="Plot", menu=Plot)
# table menu for viewing data in a table
table = tk.Menu(menubar, tearoff = 0)
table.add_command(label="View MY CSV File", command = table)
table.add_cascade(label = "View Data", menu = table)
tk.Tk.config(self, menu=table)
....
....
【问题讨论】:
标签: python tkinter menubar filedialog