我的程序有一个页面(主页),其中有 5 个单选按钮和一个“确定”按钮,我还有 5 个其他框架。我希望我的程序在单击“确定”(基于选择的单选按钮)后转到这些帧。
单击“确定”时,它将调用 lambda 并获取所选单选按钮的值,并基于此值找到代表页面名称的字符串 (veg)。
这是代码:
从 tkinter 导入 tkinter 作为 tk 从 win32print 导入 ttk 导入 StartPage
类 KBSapp(tk.Tk): def init (self, *args, **kwargs): tk.Tk. init (self, *args, **kwargs) tk.Tk.iconbitmap(self,default="icon.ico") tk.Tk.wm_title(self, "蔬菜作物病虫害") container = tk.Frame( self, width=200, height=200, bg='black') container.pack(side="top", fill="both", expand=True) container.grid_rowconfigure(0, weight=1) container.grid_columnconfigure( 0,重量=1)
self.frames = {}
for F in (StartPage,carrot,celery, potato, wheat, bean):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise( )
类 StartPage(tk.Frame): def init (self, parent, controller): tk.Frame。初始化(自己,父母)
label = tk.Label(self, width=0, height=20)
label.pack()
button2 = ttk.Button(self, command=lambda : okclick(v) ,text='OK', width=25)
button2.pack( )
v = tk.IntVar( )
self.option1 = ttk.Radiobutton(self, text="Carrot", variable=v, value=1);self.option1.pack( )
self.option2 = ttk.Radiobutton(self, text="Celery", variable=v, value=2);self.option2.pack( )
self.option3 = ttk.Radiobutton(self, text="potato", variable=v, value=3);self.option3.pack( )
self.option4 = ttk.Radiobutton(self, text="wheat", variable=v, value=4);self.option4.pack( )
self.option5 = ttk.Radiobutton(self, text="bean", variable=v, value=5);self.option5.pack( )
v.set(1) # initializing the choice
def okclick(v): global veg input1=v.get() if input1==1: veg="carrot" if input1==2: veg='celery' if input1==3: veg='potato' if input1 ==4: veg='wheat' if input1==5: veg='bean' print(veg)
类胡萝卜(tk.Frame):def init(自我,父母,控制器):tk.Frame。init (self, parent) label = tk.Label(self, width=0, height=20) label.pack( ) button3 = ttk.Button(self, command=lambda : controller.show_frame(celery) , text='celery ', width=25) button3.pack( )
芹菜类(tk.Frame):def init(自我,父母,控制器):tk.Frame。init (self, parent) label = tk.Label(self, width=0, height=20) label.pack() button4 = ttk.Button(self, command=lambda : controller.show_frame(potato) , text='potato ', width=25) button4.pack( )
土豆类(tk.Frame):def init(自我,父母,控制器):tk.Frame。init (self, parent) label = tk.Label(self, width=0, height=20) label.pack( ) button4 = ttk.Button(self, command=lambda : controller.show_frame(wheat) , text='wheat ', width=25) button4.pack( )
小麦类(tk.Frame):def init(自我,父母,控制器):tk.Frame。init (self, parent) label = tk.Label(self, width=0, height=20) label.pack( ) button4 = ttk.Button(self, command=lambda : controller.show_frame(bean) , text='bean ', width=25) button4.pack( )
类 bean(tk.Frame): def init (self, parent, controller): tk.Frame。init (self, parent) label = tk.Label(self, width=0, height=20) label.pack() button4 = ttk.Button(self, command=lambda : controller.show_frame(StartPage) , text='StartPage ', width=25) button4.pack( )
app = KBSapp() app.mainloop()