【发布时间】:2018-05-20 21:53:10
【问题描述】:
我从 python 开始,我已经用matplotlib 创建了一个很好的绘图函数。现在我想使用 tkinter 将该函数插入到 GUI 中。
正如我从 youtube 和这个论坛中了解到的那样,我应该使用课程。
不幸的是,我对此有一些问题。到目前为止我所拥有的(我没有详细了解所有内容)是:
import tkinter as tk
from tkinter import filedialog
LARGE_FONT=('Verdana',12)
class XPSPlotApp(tk.Tk):
def __init__(self, *args,**kwargs):
tk.Tk.__init__(self, *args,**kwargs)
container=tk.Frame(self)
container.pack(side='top',fill='both',expand=True)
container.grid_rowconfigure(0,weight=1)#0 is min size
container.grid_columnconfigure(0,weight=1)
#adding a menubar#
self.menuBar = tk.Menu(master=self)
self.filemenu = tk.Menu(self.menuBar, tearoff=0)
self.filemenu.add_command(label="Open", command=self.browse_file)
self.filemenu.add_command(label="Quit!", command=self.quit)
self.menuBar.add_cascade(label="File", menu=self.filemenu)
self.config(menu=self.menuBar)
self.frames={}
for F in (HRXPSPlotter, SurveyXPSPlotter):
frame=F(container,self)
self.frames[F]=frame
frame.grid(row=0,column=1,sticky='nsew')
self.show_frame(HRXPSPlotter)
def show_frame(self, cont):
frame=self.frames[cont]
frame.tkraise()
#Question if it should be here because maybe overwrites the filename form other window?
def browse_file(self):
self.filename = filedialog.askopenfilename(initialdir = "E:/Images",title = "choose your file",filetypes = (("txt files","*.txt"),("all files","*.*")))
print(self.filename)
def printFN(self):
print(self.filename)
class HRXPSPlotter(tk.Frame):
def __init__(self,parent, controller):
tk.Frame.__init__(self,parent)
lable=tk.Label(self,text='Sart Page',font=LARGE_FONT)
lable.pack(pady=10,padx=10)
#button to go to another page
button1=tk.Button(self, text='Visit Page 1',command=lambda:controller.show_frame(SurveyXPSPlotter))
button1.pack()
class SurveyXPSPlotter(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
lable=tk.Label(self,text='Page One',font=LARGE_FONT)
lable.pack(pady=10,padx=10)
button1=tk.Button(self, text='Back to Page 1',command=lambda:controller.show_frame(HRXPSPlotter))
button1.pack()
app=XPSPlotApp()
app.mainloop()
该程序有两个页面(因为我想使用不同的绘图仪)和一个可以从目录中打开文件的菜单。
- 现在我想实现一个函数,当我点击一个按钮时,它会绘制
filename+filepath,但我没有得到它的工作?问题很简单,我真的不知道我必须在哪里定义函数——在__init__中还是在子类中?还有... -
command=lambda:controller是如何工作的,它对我来说仍然很神奇! - 为什么
tk.Frame而不是tk.Tk的子类(例如pageOne)方法与__init__不同?
【问题讨论】:
-
command=lambda:controller.show_frame(SurveyXPSPlotter)就像def command(): return controller.show_frame(SurveyXPSPlotter)
标签: python class user-interface tkinter