简体   繁体   English

Python Tkinter 按钮命令出现问题

[英]Problem with Python Tkinter Button command

I wrote a login/sign up system in python using Tkinter.我使用 Tkinter 在 python 中编写了一个登录/注册系统。 The code is something like:代码类似于:

class Sign_Up:

    def __init__(self, root):
        self.root = root
        root.geometry('500x500')
        self.name = StringVar()
        ...

        label_0 = Label(root, ...)
        entry_0 = entry(root, text = name)
        ...

        self.b = Button(root, command = flag, ...)
        self.mainloop()

    def flag(self):

        name1 = self.name.get()
        ...

Flag function checks whether the username is available or passwords match and shows relative messages from tkinter.mesagebox.标志 function 检查用户名是否可用或密码是否匹配,并显示来自 tkinter.mesagebox 的相关消息。

Everything works fine and desired when I call the function below:当我在下面调用 function 时,一切正常且理想:

def signup():
    root = Tk()
    s = Sign_Up(root)

signup()

However, when I write another class Menu which is a class for a window that has 2 buttons: Sign up and Sign in and pass this function to its button command, it does not work: However, when I write another class Menu which is a class for a window that has 2 buttons: Sign up and Sign in and pass this function to its button command, it does not work:

class Menu:

    def __init__(self, root):
        self.root = root
        ...

        self b1 = (root, command = signup, ...)
        root.mainloop()

    def signup(self):
        root = Tk()
        s = Sign_Up(root)

Sign up function does not work with command and I assume that the problem is about get function in the flag function above because every time it shows a warning 'fill in the blanks' which is supposed to be displayed when the length of the entries is 0. Sign up function does not work with command and I assume that the problem is about get function in the flag function above because every time it shows a warning 'fill in the blanks' which is supposed to be displayed when the length of the entries is 0 .

As I said, flag function and sign up class works properly independently, but it does not work when I pass it to tkinter button command.正如我所说,标记 function 并注册 class 可以独立正常工作,但是当我将它传递给 tkinter 按钮命令时它不起作用。 How can I fix this?我怎样才能解决这个问题?

I solved this problem, Tk() only has to be used for the first window, for the next ones, Toplevel() should be used.我解决了这个问题,Tk() 只需要用于第一个 window,接下来的,应该使用 Toplevel()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM