当我运行代码时,框出现在左上方,大部分都在屏幕视图之外。我已经检查了代码,但无法找到导致此问题的原因。谢谢你的帮助。
from tkinter import *
import sqlite3
import LoginMenu
class LogOn:
def __init__(self, window):
self.window = window
window.title("Log On")
window.state("zoomed")
h = self.window.winfo_height()
w = self.window.winfo_width()
Center_h = h/2
Center_w = w/2
self.FrameLogOn = Frame(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)
答案 0 :(得分:0)
您的窗口尚未由几何管理器管理,因此winfo_width()
和winfo_height()
将返回1.如果您在window.update_idletasks()
之前添加h = self.window.winfo_height()
,那么问题就解决了。像这样:
window.state("zoomed")
window.update_idletasks()
h = self.window.winfo_height()
答案 1 :(得分:0)
而不是使用有点复杂的place
,如何使用pack?据我所知,这就是你想要的:
self.FrameLogOn = Frame(window, bg = "PaleTurquoise1")
self.FrameLogOn.pack(expand=True)
只需替换
h = self.window.winfo_height()
w = self.window.winfo_width()
Center_h = h/2
Center_w = w/2
self.FrameLogOn.place(x = Center_w , y = Center_h, anchor = "center")
self.FrameLogOn.pack(expand=True)
。
expand=True
选项会将内框保留在窗口的中心。
答案 2 :(得分:0)
place
可让您使用相对坐标。 relx=.5, rely=.5, anchor="center"
将小部件完全放在其主控中间。