Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
116 views
in Technique[技术] by (71.8m points)

python - Tkinter Button to check entries

i am trying to create a Button that checks information from entries. If the information is True the window should be cleared and display two new labels, else a new window should pop-up with an error message. At the moment i cant figure out why the code ends up in the else block every single time, no matter what I type into the entries. Is someone able to explain why and how i could solve this problem? Maybe the "Entry.get()" method works different than i expected?..

from tkinter import *


class FirstPrototype:
    def __init__(self, top=None):
        self.top = top
        top.title("Prototype")
        top.configure(bg="black", highlightbackground="grey", highlightcolor="darkgrey")

        wWidth = top.winfo_reqwidth()
        wHeight = top.winfo_reqheight()

        x = int(top.winfo_screenwidth() / 2 - (wWidth / 2))
        y = int(top.winfo_screenheight() / 3 - (wHeight / 2))

        top.geometry(f"+{x}+{y}")
        top.minsize(300, 132)

        self.Account = Button(text="Account", command=self.log_reg).pack()

    def log_reg(self):
        self.clear_window()

        Label(text="ID:", bg="darkgrey").pack(fill=X)
        e1 = Entry()
        e1.pack(fill=X)
        Label(text="PW:", bg="darkgrey").pack(fill=X)
        e2 = Entry()
        e2.pack(fill=X)
        Button(text="Login", command=self.check_id).pack(fill=X)
        Button(text="Register").pack(fill=X)

        return e1, e2

    def clear_window(self):
        for widget in top.winfo_children():
            widget.destroy()

    def check_id(self):
        e1, e2 = self.log_reg()
        u_id = e1.get()
        u_pw = e2.get()

        if u_id == "Qwe123" and u_pw == "Qwe123":
            self.clear_window()

            Label(top, text="Nickname:", bg="darkgrey").pack(fill=X)
            Label(top,text="Examplenickname",bg="darkgrey").pack(fill=X)

        else:
            error_w = Tk()
            error_w.title("ERROR")
            Label(error_w, text="ERROR").place(relx=0.5, rely=0.5, anchor=CENTER)


top = Tk()
exa_gui = FirstPrototype(top)
top.mainloop()
question from:https://stackoverflow.com/questions/65846903/tkinter-button-to-check-entries

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For explanation refer my commentt

Here:

from tkinter import *


class FirstPrototype:
    def __init__(self, top=None):
        self.top = top
        top.title("Prototype")
        top.configure(bg="black", highlightbackground="grey", highlightcolor="darkgrey")

        wWidth = top.winfo_reqwidth()
        wHeight = top.winfo_reqheight()

        x = int(top.winfo_screenwidth() / 2 - (wWidth / 2))
        y = int(top.winfo_screenheight() / 3 - (wHeight / 2))

        top.geometry(f"+{x}+{y}")
        top.minsize(300, 132)

        self.Account = Button(text="Account", command=self.log_reg).pack()
        self.e1 = None
        self.e2 = None
        
    def log_reg(self):
        #self.clear_window()

        Label(text="ID:", bg="darkgrey").pack(fill=X)
        self.e1 = Entry()
        self.e1.pack(fill=X)
        Label(text="PW:", bg="darkgrey").pack(fill=X)
        self.e2 = Entry()
        self.e2.pack(fill=X)
        Button(text="Login", command=self.check_id).pack(fill=X)
        Button(text="Register").pack(fill=X)


    def clear_window(self):
        for widget in top.winfo_children():
            widget.destroy()

    def check_id(self):
        #e1, e2 = self.log_reg()
        u_id = self.e1.get()
        u_pw = self.e2.get()

        print(u_id)
        
        if u_id == "Qwe123" and u_pw == "Qwe123":
            self.clear_window()

            Label(top, text="Nickname:", bg="darkgrey").pack(fill=X)
            Label(top,text="Examplenickname",bg="darkgrey").pack(fill=X)

        else:
            error_w = Tk()
            error_w.title("ERROR")
            Label(error_w, text="ERROR").place(relx=0.5, rely=0.5, anchor=CENTER)


top = Tk()
exa_gui = FirstPrototype(top)
top.mainloop()


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...