Tkinter通过菜单栏按钮存储文件名



我已经创建了一些函数,可以在使用tkinter制作的GUI中显示csv文件的图形和表格。

我有一个menubar,带有导入按钮、绘图按键和表格键。绘图表格按钮可以分别成功绘制csv文件的图形和表格。

我想做的是,当用户选择导入按钮时,他们会选择自己选择的文件。然后,如果他们碰巧选择了绘图按钮,绘图功能将对他们从导入中选择的文件起作用。此外,如果他们碰巧选择了按钮,表函数将对他们从导入中选择的文件起作用。

我创建了一个名为openfile()的文件打开函数,它会记住打开的文件的名称。

问题是我不知道如何使用menubaropenfile(),所以当单击导入按钮时,我的应用程序会存储文件名。

关于我该怎么做,有什么建议吗?

这是我为menubaropenfile():编写的代码

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

首先,我建议您采用面向对象方法来拆分每个组件的行为。

这样,你就有了一个类应用程序,在那里你可以初始化应用程序的主要组件:

class App(tk.Tk):
def __init__(self,parent):
Tk.__init__(self,parent)
self.parent = parent
self.initialize()
def initialize(self):
...
...
# Menubar
self.menubar = MyMenuBar(self)
# Filemenu
self.filemenu = MyFileMenu(self, menubar)
# Plot
self.plot = MyPlot(self, menubar)
# The rest for all the other components
...
def get_import_filename(self):
# Ask the child for the filename and return it
return self.filemenu.get_filename()

然后定义你的每一个对象:

class MyPlot(tk.Menu):
def __init__(self, parent, menubar):
tk.Menu.__init__(self, menubar, tearoff=0)
self.parent = parent
self.initialize()
def initialize(self):
self.add_command(label="Plot My CSV File", command= self.popupgraph)  
def popupgraph(self):
# Ask the parent for the filename
filename = self.parent.get_import_filename()
# Do whatever with the filename...like open a file
class MyFileMenu(tk.Menu):
def __init__(self, parent, menubar):
tk.Menu.__init__(self, menubar, tearoff=0)
self.parent = parent
self.initialize()
def initialize(self):
self.add_command(label="Import a CSV File", command = file_opener)
def file_opener(self):
# Your file opener function goes here
...
# At the end, save the imported file:
self.filename = filename
def get_filename(self):
return self.filename

最后,让main运行它:

def main():
app = App(None)
app.title('Auto login')
app.mainloop()
if __name__ == "__main__":
main()

otorrillas解决方案运行良好。

我找到了另一种简单的方法来解决这个问题。为了让GUI记住从import中选择的文件,请将文件名附加到列表中。然后,调用列表元素上的popupgraph()viewcsv()。这是我的文件打开函数的新代码。

file_list = []
def openfile():
name= askopenfilename() 
file_list.append(name[19:])

我不明白你试图做一些事情的方式(或者在某些情况下你为什么要做),但这里有一个可运行的示例,它在简单tkinter应用程序窗口的左上角创建了一个简单的Choices级联菜单。希望你;;能够将其调整并集成到您的项目代码中。

我把导入CSV文件的命令file_openerfunction作为一个类方法,并让它调用openfile()函数,因为这样返回的值(文件名)可以作为属性存储在self.filename中。这将允许在创建后在其他命令选项中使用它(这样他们就知道要操作什么文件)。

try:
import Tkinter as tk
from tkFileDialog import askopenfilename
except ModuleNotFoundError:   $ Python 3
import tkinter as tk
from tkinter.filedialog import askopenfilename
def openfile():
name = askopenfilename()
return name[19:]
class MyApp(tk.Frame):
def __init__(self, master=None):
tk.Frame.__init__(self, master)
self.master.title("MyApp")
self.pack(side="top", fill="both", expand=True)
self.grid_rowconfigure(0, weight=1)
self.grid_columnconfigure(0, weight=1)
top = self.winfo_toplevel()
self.menubar = tk.Menu(top)
top['menu'] = self.menubar
self.submenu = tk.Menu(self.menubar)
self.menubar.add_cascade(label='Choices', menu=self.submenu)
self.submenu.add_command(label="Import a CSV File",
command=self.file_openerfunction)
self.submenu.add_command(label="Plot My CSV File",
command=self.popupgraph)
self.submenu.add_command(label="View MY CSV File",
command=self.table)
def file_openerfunction(self):
self.filename = openfile()
def popupgraph(self): pass
def table(self): pass
app = MyApp()
app.mainloop()

我还建议您阅读并遵循PEP 8-Style Guide for Python Code,这将有助于标准化您编写的代码并提高其可读性。

最新更新