Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我可以在方法中使用lambda按钮命令吗?_Python_Class_Methods_Lambda_Tkinter - Fatal编程技术网

Python 我可以在方法中使用lambda按钮命令吗?

Python 我可以在方法中使用lambda按钮命令吗?,python,class,methods,lambda,tkinter,Python,Class,Methods,Lambda,Tkinter,我用python编写代码已经很短时间了,因此我并不完全理解该语言的逻辑,尤其是在使用Tkinter时。我目前正在开发一个导航系统,该系统是我在登录页面专门开发的。以下是我的问题代码: class Application(Tk.Tk): def __init__(self, *args, **kwargs): Tk.Tk.__init__(self, *args, **kwargs) container = Tk.Frame(self) c

我用python编写代码已经很短时间了,因此我并不完全理解该语言的逻辑,尤其是在使用Tkinter时。我目前正在开发一个导航系统,该系统是我在登录页面专门开发的。以下是我的问题代码:

class Application(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 (createLogin, createHome, createViewer, createAnalyse):

            frame = F(container, self)

            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(createLogin)

    def show_frame(self, cont):

        frame = self.frames[cont]
        frame.tkraise()

    def connectToDataBase(self):
        cnxn = pyodbc.connect('DRIVER={Microsoft Access Driver (*.mdb,      *.accdb)};DBQ=C:\Users\Public\dbsDetectorBookingSystem.accdb')
        cursor = cnxn.cursor()

        cursor.execute("SELECT * FROM tblUsers")
        userRows = cursor.fetchall

        cursor.execute("SELECT * FROM tblRuns")
        runsRows = cursor.fetchall



class createLogin(Tk.Frame):

    def __init__(self, parent, controller):
        Tk.Frame.__init__(self,parent)

        topFrame= Tk.Frame(self)
        topFrame.pack(side="top")

        leftFrame = Tk.Frame(topFrame)
        leftFrame.pack(side="left")

        nameLabel = Tk.Label(leftFrame, text = "Username")
        nameLabel.pack(side="top", padx = 5, pady = 10)

        passwordLabel = Tk.Label(leftFrame, text = "Password")
        passwordLabel.pack(side="top", padx = 5, pady = 10)

        rightFrame = Tk.Frame(topFrame)
        rightFrame.pack(side="left")

        nameText = Tk.Text(rightFrame, height=1, width = 30)
        nameText.pack(side="top", padx = 5, pady = 10)

        passwordText = Tk.Text(rightFrame, height=1, width = 30)
        passwordText.pack(side="top", padx = 5, pady = 10)

        botFrame = Tk.Frame(self)
        botFrame.pack(side="top")

        Login = Tk.Button(botFrame, text="Login")
        Login["command"] = lambda: self.checkLogin()
        Login.pack(side="left", padx = 5, pady = 10)
我想做的是让login按钮调用一个方法,该方法可以从read-in数据库验证带有用户名和密码的文本框,如果它们是正确的,那么应该调用“show_frame”方法。我可以实现验证,但是每当我创建一个方法来尝试调用“show_frame”时,我必须创建一个应用程序类的新实例,这远远不是我想要的,我真正想要做的是触发该方法,在应用程序类的当前实例中传递“createHome”


非常感谢您在这方面提供的任何帮助。很抱歉,您是一个新手。

您的
createLogin
类已经有了对主应用程序的引用,因此我不理解您为什么认为必须“创建应用程序类的新实例”

我认为您需要做的就是保存
controller
的实例,并使用它调用
show\u frame
方法

class createLogin(Tk.Frame):

    def __init__(self, parent, controller):
        Tk.Frame.__init__(self,parent)
        self.controller = controller
        ...
        Login = Tk.Button(botFrame, text="Login", command=self.checkLogin)
        ...
    def checkLogin(self):
        ...
        if correct_password:
            self.controller.show_frame(createHome)

多亏了这一点,就像我说的我对该语言(以及GUI编程)是新手一样,我在网上发现的关于使用另一个类的方法的其他事情,刚才说的是创建该类的新实例,这就是这个想法的来源。你帮了大忙。