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

使用类定义时Tkinter窗口无法打开的问题求助

Fixing Your Tkinter Login Window Issue

Hey there, let's get that login window up and running! I found a handful of small but critical mistakes in your code that are preventing the window from opening. Here's a breakdown of the issues and the corrected code:

Key Problems in Your Original Code

  • Incorrect assignment in __init__: You wrote self.root-root which is a subtraction operation, not an assignment. It should be self.root = root to store the main window reference in your class.
  • Invalid geometry string: Tkinter's geometry() method uses a lowercase x to separate width and height, not an asterisk *. So change "1550*800+0+0" to "1550x800+0+0".
  • Wrong main window creation: root=tk() won't work—tk is the module, you need to call the Tk() constructor to create a window: root = tk.Tk().
  • Incorrect mainloop() call: Your Login_window instance (app) doesn't have a mainloop() method. You need to call this method on the main window instance (root).

Corrected Code

from tkinter import *
from tkinter import ttk
import tkinter as tk

class Login_window:
    def __init__(self, root):
        self.root = root  # Fixed assignment
        self.root.title("Login")
        self.root.geometry("1550x800+0+0")  # Fixed geometry format

if __name__ == "__main__":
    root = tk.Tk()  # Correct window creation
    app = Login_window(root)
    root.mainloop()  # Call mainloop on the root window

Just run this corrected code, and your login window should pop up as expected!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.30 11:32:41