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

Tkinter登录窗口运行无报错但无法显示,求错误排查

问题分析与修正方案

核心错误及修复点

  • 构造函数名称写错:类的构造函数必须是__init__,你写成了__int__,导致实例化类时完全没有执行窗口创建的逻辑,这是窗口不显示的根本原因。
  • 组件未绑定父窗口:self.loginlabel这个Label没有指定父窗口self.loginwindow,即使构造函数正常执行,该标签也不会显示在目标窗口上。
  • 密码获取对象错误:登录方法里用self.loginpasswordlabel.get()获取密码,但Label组件没有get()方法,应该改为从密码输入框self.loginpasswordentry获取内容。
  • 输入框清空方式错误:Entry组件清空需要用delete(0, END),而不是config(text=""),后者对Entry组件无效。

修正后的完整代码

from tkinter import *

class Login:
    def __init__(self):  # 修正构造函数名称
        self.user ={'Hello':'123'}
        self.loginwindow = Tk()
        self.loginwindow.geometry("500x300")
        self.loginwindow.resizable(width= False, height= False)

        # 给loginlabel指定父窗口
        self.loginlabel = Label(self.loginwindow, text="Login",font= ("arial",50))
        self.loginausgabe = Label(self.loginwindow)
        self.loginusernamelabel = Label(self.loginwindow, text="Username",font= ("arial",12))
        self.loginusernameentry = Entry(self.loginwindow,font= ("arial",12))
        self.loginpasswordlabel = Label(self.loginwindow,text="Password",font= ("arial",12))
        self.loginpasswordentry = Entry(self.loginwindow,font= ("arial",12), show="*")
        self.loginbtn = Button(self.loginwindow,text="login",width= 38, command=self.Login)

        self.loginlabel.place(x="140",y="0")
        self.loginausgabe.place(x="140",y="140")
        self.loginusernamelabel.place(x="140",y ="80")
        self.loginusernameentry.place(x="230",y="80")
        self.loginpasswordlabel.place(x="140",y ="100")
        self.loginpasswordentry.place(x="230",y="100")
        self.loginbtn.place(x="140",y="120")
        self.loginwindow.mainloop()

    def Login(self):
        loginusernameentry = self.loginusernameentry.get()
        if loginusernameentry in self.user.keys():
            # 修正为从密码输入框获取内容
            if self.loginpasswordentry.get() == self.user[loginusernameentry]:
                self.loginausgabe.config(text="Welcome!")
            else: 
                self.loginausgabe.config(text="Invalid password!")
        else: 
            self.loginausgabe.config(text="Unknown User!")
        # 修正输入框清空逻辑
        self.loginusernameentry.delete(0, END)
        self.loginpasswordentry.delete(0, END)

l = Login()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 11:36:01