属性错误:“ str”对象没有属性“ tk”

时间:2019-09-10 13:15:32

标签: python tkinter attributeerror

我开始使用Tkinter用Python开发一个登录/注册系统。我已经到达要尝试注册按钮的位置,然后打开注册顶层窗口。但是,当我单击“注册”按钮时,只会打开一个空白窗口,并且python返回错误。

def register():
    register = Tk()
    register_screen = Toplevel
    register_screen.title("Register")
    register_screen.geometry("300x250")

    username = StringVar()
    password = StringVar()

    username_label = Label(register_screen, text="Username")
    username_label.pack()
    username_entry = Entry(register_screen, textvariable=username)
    username_entry.pack()

    password_label = Label(register_screen, text="Password")
    password_label.pack()
    password_entry = Entry(register_screen, textvariable=password, show='*')
    password_entry.pack()

    Label(register_screen, text="").pack()

    Button(register_screen, text="Register", width=10, height=1).pack()

预期结果:注册窗口打开,并带有输入框

实际结果:空白窗口,其中包含以下Python错误代码:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\Theo\AppData\Local\Programs\Python\Python36\lib\tkinter\__init__.py", line 1702, in __call__
    return self.func(*args)
  File "C:/Users/Theo/Desktop/Calorie Counter/caloriecounter.py", line 14, in register
    register_screen.title("Register")
  File "C:\Users\Theo\AppData\Local\Programs\Python\Python36\lib\tkinter\__init__.py", line 1982, in wm_title
    return self.tk.call('wm', 'title', self._w, string)
AttributeError: 'str' object has no attribute 'tk'

1 个答案:

答案 0 :(得分:0)

您的空白屏幕是我不需要的注册窗口,但是您需要使Toplevel()成为对象调用。

systemctl daemon-reload
systemctl restart kubelet