简体   繁体   中英

Attribute Error: 'str' object has no attribute 'tk'

I'm beginning to develop a login/register system in Python with Tkinter. I have got to the point where I'm trying to make the register button then open a registration top level window. However when I click on the Register button just a blank window opens and python returns an error.

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()

Expected Result: Registration window to open with entry boxes

Actual Result: Blank window with the following error code in 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'

Your blank screen is the register window which I don't believe you need, but you need to make Toplevel() an object call.

def register():
    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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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