使用类定义时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 wroteself.root-rootwhich is a subtraction operation, not an assignment. It should beself.root = rootto store the main window reference in your class. - Invalid geometry string: Tkinter's
geometry()method uses a lowercasexto 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—tkis the module, you need to call theTk()constructor to create a window:root = tk.Tk(). - Incorrect
mainloop()call: YourLogin_windowinstance (app) doesn't have amainloop()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
相关产品推荐
相关产品推荐

