You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python 3.11中Tkinter创建TopLevel触发TypeError错误求助

Tkinter创建TopLevel组件时触发TypeError错误

错误信息

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Program Files\Python310\lib\tkinter\__init__.py", line 1921, in __call__
    return self.func(*args)
  File "c:\Users\kanne\OneDrive - csbme.de\Schuljahr2\Software\test.py", line 34, in login   
    pw_change()
  File "c:\Users\kanne\OneDrive - csbme.de\Schuljahr2\Software\test.py", line 7, in pw_change
    window2 = tk.Toplevel(window)
  File "C:\Program Files\Python310\lib\tkinter\__init__.py", line 2653, in __init__
    self.title(root.title())
TypeError: 'str' object is not callable

我的代码

import tkinter as tk

password_login = "123"
email_login = "asd"
def pw_change():

    window2 = tk.Toplevel(window)
    window2.title("Change")
    frame = tk.Frame(window2)
    pwchange_label = tk.Label(frame,text="Neues Passwort")
    pwchange_entry = tk.Entry(frame)

    pwagain_label = tk.Label(frame,text="Erneut eingeben")
    pwagain_entry = tk.Entry(frame)

    button = tk.Button(window2,text="Änder Passwort")

    pwchange_label.grid(row=0,column=0)
    pwchange_entry.grid(row=0,column=1)
    pwagain_label.grid(row=1,column=0)
    pwagain_entry.grid(row=1,column=1)

    frame.pack()
    button.pack()
    window2.mainloop()


def login():
    print(farbe)
    if password_login == passwort_entry.get() and email_login == login_entry.get():
        text.config(text="Login erfolgreich", fg="green")
        print(var.get())
        if var.get() == True:
            pw_change()

    else:

        if farbe.get() == "blue":
                
            text.config(text="Login fehlgeschlagen", fg="blue")
        elif farbe.get() == "green":
            text.config(text="Login fehlgeschlagen", fg="green")


window = tk.Tk()
window.title = "Klausur"
farbe = tk.StringVar()
var = tk.BooleanVar()
image = tk.Label(window)
bild = tk.PhotoImage(file="1.png")
image["image"] = bild
frame = tk.Frame(window)
login_text = tk.Label(frame, text="E-Mail")
login_entry = tk.Entry(frame)
farbe.set("green")
radio_b = tk.Radiobutton(window, text="Blau", variable=farbe, value="blue")
radio_b2 = tk.Radiobutton(window,text="Grün", variable=farbe,value="green")


login_text.grid(row=0, column = 0,padx=5,pady=5)
login_entry.grid(row=0, column = 1,padx=5,pady=5)

passwort_text = tk.Label(frame, text="Passwort")
passwort_entry = tk.Entry(frame,show="*")

passwort_text.grid(row=1, column = 0,padx=5,pady=5)
passwort_entry.grid(row=1, column = 1,padx=5,pady=5)
text = tk.Label(window)
button = tk.Button(window,text="Login",command = login)

pw_change1 = tk.Checkbutton(window, text="Passwort ändern",variable=var)
image.pack()
frame.pack()
radio_b.pack()
radio_b2.pack()
pw_change1.pack()
button.pack()
text.pack()

window.mainloop()

已尝试的解决方法

  • 将Python版本从3.10升级到3.11.2
  • 改用from tkinter import *的导入方式并移除所有tk.前缀

问题原因与修复方案

核心错误

设置主窗口标题时的写法错误:

window.title = "Klausur"

这行代码直接把window对象的title方法覆盖成了字符串。当Tkinter初始化TopLevel组件时,会调用root.title()获取主窗口标题,但此时title已经是字符串而非可调用方法,因此触发TypeError。

修复方法:将赋值语句改为方法调用:

window.title("Klausur")

额外优化点

  1. 删除多余的window2.mainloop():主窗口已经启动了事件循环,嵌套调用会导致界面响应异常,直接删除该行即可。
  2. 为密码修改按钮绑定功能:当前"Änder Passwort"按钮无实际功能,可以添加函数校验两次输入的密码是否一致,并更新登录密码变量。

内容的提问来源于stack exchange,提问作者Leon Kannenberg

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 13:21:28