Tkinter登录窗口未居中求助:窗口偏移至屏幕左上角
问题原因与解决方案
我一眼就看出问题出在哪了——你在调用window.state("zoomed")之后立刻获取窗口的高度和宽度,但这时候窗口其实还没完成最大化的布局动作,拿到的h和w还是窗口初始状态的小尺寸,不是最大化后的真实尺寸。这样算出来的Center_h和Center_w完全不对,导致登录Frame被放在了实际最大化窗口的右上角区域,看起来就像是跑到左上角且内容超出可视范围了。
下面给你几个可行的解决办法:
方法一:延迟获取窗口尺寸
用after()方法等几百毫秒,让窗口完成最大化后再计算中心位置:
from tkinter import * import sqlite3 import LoginMenu class LogOn: def __init__(self, window): self.window = window window.title("Log On") window.state("zoomed") # 延迟执行布局逻辑,给窗口时间完成最大化 self.window.after(200, self.setup_frame) def setup_frame(self): h = self.window.winfo_height() w = self.window.winfo_width() Center_h = h/2 Center_w = w/2 self.FrameLogOn = Frame(self.window, bg = "PaleTurquoise1") self.FrameLogOn.place(x = Center_w , y = Center_h, anchor = "center") # 以下控件创建代码保持不变 self.lbl_TrainerID = Label(self.FrameLogOn, text = "TrainerID:", bg = "PaleTurquoise1", font =("Arial","16"), width = 15) self.lbl_TrainerID.grid(row = 0, column = 0) self.ent_TrainerID = Entry(self.FrameLogOn, bg = "PaleTurquoise1", font =("Arial","16")) self.ent_TrainerID.grid(row = 0, column = 1) self.lbl_TrainerIDError = Label(self.FrameLogOn, text = "*TrainerID Not Found" ,bg = "PaleTurquoise1", font =("Arial","16"), fg = "PaleTurquoise1", width = 15) self.lbl_TrainerIDError.grid(row = 0, column = 2) self.lbl_Password = Label(self.FrameLogOn, text = "Password:" ,bg = "PaleTurquoise1", font =("Arial","16"), width = 15) self.lbl_Password.grid(row = 1, column = 0) self.ent_Password = Entry(self.FrameLogOn, bg = "PaleTurquoise1", font =("Arial","16")) self.ent_Password.grid(row = 1, column = 1) self.lbl_PasswordError = Label(self.FrameLogOn, text = "*Incorrect Password" ,bg = "PaleTurquoise1", font =("Arial","16"), fg = "PaleTurquoise1", width = 15) self.lbl_PasswordError.grid(row = 1, column = 2) self.btn_LogIn = Button(self.FrameLogOn, text = "Log In", bg = "PaleTurquoise1", font =("Arial", "16"), width = 15, command = self.LogIn) self.btn_LogIn.grid(row = 2, column = 0, columnspan = 3) def LogIn(self): # 保留你的登录逻辑 pass if __name__ == "__main__": root = Tk() app = LogOn(root) root.mainloop()
方法二:用布局管理器自动居中(更推荐)
其实不用手动计算位置,直接用pack让Frame自动居中,这样不管窗口怎么缩放,Frame都会保持在中心位置:
from tkinter import * import sqlite3 import LoginMenu class LogOn: def __init__(self, window): self.window = window window.title("Log On") window.state("zoomed") # 用pack让Frame自动居中,添加padding避免贴紧窗口边缘 self.FrameLogOn = Frame(window, bg = "PaleTurquoise1") self.FrameLogOn.pack(expand=True, padx=20, pady=20) # 以下控件创建代码保持不变 self.lbl_TrainerID = Label(self.FrameLogOn, text = "TrainerID:", bg = "PaleTurquoise1", font =("Arial","16"), width = 15) self.lbl_TrainerID.grid(row = 0, column = 0) self.ent_TrainerID = Entry(self.FrameLogOn, bg = "PaleTurquoise1", font =("Arial","16")) self.ent_TrainerID.grid(row = 0, column = 1) self.lbl_TrainerIDError = Label(self.FrameLogOn, text = "*TrainerID Not Found" ,bg = "PaleTurquoise1", font =("Arial","16"), fg = "PaleTurquoise1", width = 15) self.lbl_TrainerIDError.grid(row = 0, column = 2) self.lbl_Password = Label(self.FrameLogOn, text = "Password:" ,bg = "PaleTurquoise1", font =("Arial","16"), width = 15) self.lbl_Password.grid(row = 1, column = 0) self.ent_Password = Entry(self.FrameLogOn, bg = "PaleTurquoise1", font =("Arial","16")) self.ent_Password.grid(row = 1, column = 1) self.lbl_PasswordError = Label(self.FrameLogOn, text = "*Incorrect Password" ,bg = "PaleTurquoise1", font =("Arial","16"), fg = "PaleTurquoise1", width = 15) self.lbl_PasswordError.grid(row = 1, column = 2) self.btn_LogIn = Button(self.FrameLogOn, text = "Log In", bg = "PaleTurquoise1", font =("Arial", "16"), width = 15, command = self.LogIn) self.btn_LogIn.grid(row = 2, column = 0, columnspan = 3) def LogIn(self): # 保留你的登录逻辑 pass if __name__ == "__main__": root = Tk() app = LogOn(root) root.mainloop()
方法二更值得推荐,Tkinter的布局管理器本身就是用来解决这类位置适配问题的,比手动计算位置更稳定,窗口缩放时也能自动调整布局。
内容的提问来源于stack exchange,提问作者evansciaran
相关产品推荐
相关产品推荐

