2

所以我有以下内容,效果很好:

import tkinter as tk

class App(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)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}
        for F in (LoginPage, ProjPage):
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, rowspan=12, columnspan=6, sticky="nsew")

        self.show_frame(LoginPage)

    def show_frame(self, c):
        frame = self.frames[c]
        frame.tkraise()

class LoginPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        Btn = tk.Button(self, text="Sign In", command=lambda: controller.show_frame(ProjPage))

但我希望最后一个按钮命令位于单独的函数中,因此我可以先进行一些评估:

        Btn = tk.Button(self, text="Sign In", command=self.signIn)

    def signIn(self):
        # do some stuff here
        self.controller.show_frame(ProjPage)

这不起作用;无论我尝试通过控制器还是使用 lambda,似乎都没有任何效果 >.< 我没有得到什么?

4

2 回答 2

7

您似乎没有controllerself. 把它放在那里__init__LoginPage像这样:

self.controller = controller
于 2013-03-05T23:05:18.420 回答
0

我的程序有一个页面(主页),其中有 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()

于 2017-10-26T17:59:41.343 回答