无法使用get()方法从弹出窗口中获取数据

时间:2018-04-04 16:09:32

标签: python python-3.x tkinter popup

好的,所以我一直在研究这一课程,我试图在允许用户编辑他们的详细信息之前实施登录检查。但是,出于某种原因,我无法使用get()方法从弹出窗口的输入框中获取数据。它不会识别输入框,因为它们没有在checkLogin函数中定义。我不确定如何正确引用条目,因为它们在弹出窗口中。这就是我到目前为止所拥有的:

import tkinter as tk

def popupmsg(window,msg):
        popup = tk.Tk()
        popup.wm_title("Alert")
        messageLabel = tk.Label(popup, text=msg, font=MEDIUM_FONT)
        messageLabel.grid(row=1, sticky="nsew")
        okayButton = tk.Button(popup, text="Okay", command = popup.destroy)
        okayButton.grid(row=2, sticky="nsew")
        popup.mainloop()

def checkLogin(window):
        username = usernameEntry.get()
        password = passwordEntry.get()
        if username == "USERNAME" and password == "PASSWORD":
            popupmsg(window, "Login Successful")
            usernameEntry.delete(0, "end") #clears username from login
            passwordEntry.delete(0, "end") #clears password from login
        else:
            popupmsg(window, "Invalid Login") #presents error message if login is incorrect

def popupquery(window):
    popup = tk.Tk()
    popup.wm_title("Login Check")

    messageLabel = tk.Label(popup, text="Enter your username and password to edit details.", font=MEDIUM_FONT)
    messageLabel.grid(row=1, sticky="nsew")

    usernameLabel = tk.Label(popup, text="Username: ", font=MEDIUM_FONT)
    usernameLabel.grid(row=2, sticky="nsew")
    usernameVar = tk.StringVar(popup)
    usernameEntry = tk.Entry(popup, textvariable=usernameVar)
    usernameEntry.grid(row=3,column=0)

    passwordLabel = tk.Label(popup, text="Password:", font=MEDIUM_FONT)
    passwordLabel.grid(row=4, sticky="nsew")
    passwordVar = tk.StringVar(popup)
    passwordEntry = tk.Entry(popup, textvariable=passwordVar)
    passwordEntry.grid(row=5,column=0)

    okayButton = tk.Button(popup, text="Okay", command = lambda: checkLogin(window))
    okayButton.grid(row=6, sticky="nsew")
    popup.mainloop()

MEDIUM_FONT = ("Berlin Sans FB", 12)
LARGE_FONT = ("Berlin Sans FB", 16)

window = tk.Tk()

titleLabel = tk.Label(window, text="View Stylist", font=LARGE_FONT, bg="#FFC0CB")
titleLabel.grid(columnspan = 4)

#searches record using entered data
editButton = tk.Button(window, text="Edit Personal Profile",
                        command=lambda:popupquery(window))
editButton.grid(row=2, column=2, sticky="ew")

window.mainloop()

2 个答案:

答案 0 :(得分:1)

将其声明为全局变量

def popupquery(window):
    global usernameEntry, passwordEntry
    ------------

那么这个功能也是

def checkLogin(window):
    global usernameEntry, passwordEntry
    -------------

您应该使用Toplevel而不是一次致电Tkmainloop

您的完整代码

import tkinter as tk


 def popupmsg(window,msg):
        popup = tk.Toplevel()
        popup.wm_title("Alert")
        messageLabel = tk.Label(popup, text=msg, font=MEDIUM_FONT)
        messageLabel.grid(row=1, sticky="nsew")
        okayButton = tk.Button(popup, text="Okay", command = popup.destroy)
        okayButton.grid(row=2, sticky="nsew")


def checkLogin(window):

    global usernameEntry, passwordEntry
    username = usernameEntry.get()
    password = passwordEntry.get()
    if username == "USERNAME" and password == "PASSWORD":
       popupmsg(window, "Login Successful")
       usernameEntry.delete(0, "end") #clears username from login
       passwordEntry.delete(0, "end") #clears password from login
    else:
        popupmsg(window, "Invalid Login") #presents error message if login is incorrect


def popupquery(window):
    global usernameEntry, passwordEntry
    popup = tk.Toplevel()
    popup.wm_title("Login Check")

    messageLabel = tk.Label(popup, text="Enter your username and password to edit details.", font=MEDIUM_FONT)
    messageLabel.grid(row=1, sticky="nsew")

    usernameLabel = tk.Label(popup, text="Username: ", font=MEDIUM_FONT)
    usernameLabel.grid(row=2, sticky="nsew")
    usernameVar = tk.StringVar(popup)
    usernameEntry = tk.Entry(popup, textvariable=usernameVar)
    usernameEntry.grid(row=3,column=0)

    passwordLabel = tk.Label(popup, text="Password:", font=MEDIUM_FONT)
    passwordLabel.grid(row=4, sticky="nsew")
    passwordVar = tk.StringVar(popup)
    passwordEntry = tk.Entry(popup, textvariable=passwordVar)
    passwordEntry.grid(row=5,column=0)

    okayButton = tk.Button(popup, text="Okay", command = lambda: checkLogin(window))
    okayButton.grid(row=6, sticky="nsew")



MEDIUM_FONT = ("Berlin Sans FB", 12)
LARGE_FONT = ("Berlin Sans FB", 16)

window = tk.Tk()

titleLabel = tk.Label(window, text="View Stylist", font=LARGE_FONT, 
bg="#FFC0CB")
titleLabel.grid(columnspan = 4)

#searches record using entered data
editButton = tk.Button(window, text="Edit Personal Profile",
                    command=lambda:popupquery(window))
editButton.grid(row=2, column=2, sticky="ew")

window.mainloop()

答案 1 :(得分:0)

迟早你需要学习使用OOP方法在课堂上写作。

我重新构建了要在类中编写的代码,以说明如何使用类属性和类方法更清晰,更容易处理。

我已经为这里不需要它们的小部件删除了不必要的变量名。如果您计划稍后编辑/更新此窗口小部件,则实际上只需要将窗口小部件分配给变量名称。如果窗口小部件是静态的并且不需要更改,那么您可以放弃变量名称。

现在已经将所有内容都移到了一个类中,我们不需要使用global,我们可以稍微清理一下代码。它使阅读和更新更容易。

在编写tkinter GUI时,您实际上只需要1个Tk()实例。不应使用Tk()打开新窗口,而应使用Toplevel(),因为这是它的设计目标。

另一个简单的清理是删除lambda表达式,因为在这个类结构中我们不需要在这里使用lambda。有时您可能需要/想要在类中使用lambda,但在这种情况下不需要它。

最后一次改变。即使你可以在这里使用textvariable,也没有真正的理由使用它。在这种情况下,它没有帮助或伤害你,所以你可以保留textvariable参数并在输入字段上简单地使用get()

阅读以下代码,如果您有任何疑问,请与我们联系。

import tkinter as tk


class App(tk.Frame):

    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, parent, *args, **kwargs)

        self.MEDIUM_FONT = ("Berlin Sans FB", 12)
        self.LARGE_FONT = ("Berlin Sans FB", 16)
        self.root = parent

        tk.Label(self, text="View Stylist", font=self.LARGE_FONT, bg="#FFC0CB").grid(columnspan = 4)
        tk.Button(self, text="Edit Personal Profile", command=self.popupquery).grid(row=2, column=2, sticky="ew")

    def popupmsg(self, msg):
            popup = tk.Toplevel(self.root)
            popup.title("Alert")
            tk.Label(popup, text=msg, font=self.MEDIUM_FONT).grid(row=1, sticky="nsew")
            tk.Button(popup, text="Okay", command = popup.destroy).grid(row=2, sticky="nsew")

    def checkLogin(self):
            if self.usernameEntry.get() == "USERNAME" and self.passwordEntry.get() == "PASSWORD":
                self.popupmsg("Login Successful")
                self.usernameEntry.delete(0, "end")
                self.passwordEntry.delete(0, "end")
            else:
                self.popupmsg("Invalid Login")

    def popupquery(self):
        popup = tk.Toplevel(self.root)
        popup.title("Login Check")

        tk.Label(popup, text="Enter your username and password to edit details.", font=self.MEDIUM_FONT).grid(row=1, sticky="nsew")
        tk.Label(popup, text="Username: ", font=self.MEDIUM_FONT).grid(row=2, sticky="nsew")
        tk.Label(popup, text="Password:", font=self.MEDIUM_FONT).grid(row=4, sticky="nsew")

        self.usernameEntry = tk.Entry(popup)
        self.usernameEntry.grid(row=3,column=0)
        self.passwordEntry = tk.Entry(popup)
        self.passwordEntry.grid(row=5,column=0)

        tk.Button(popup, text="Okay", command = self.checkLogin).grid(row=6, sticky="nsew")


if __name__ == "__main__":
    root = tk.Tk() 
    App(root).pack()
    tk.mainloop()