【发布时间】:2023-04-03 10:48:01
【问题描述】:
我希望这个条目栏和稍后我将添加到框架中的其他内容正确居中,我收到了这个应该可以工作的代码,但事实并非如此。
import tkinter as tk
import math
import time
root = tk.Tk()
root.geometry()
root.attributes("-fullscreen", True)
exit_button = tk.Button(root, text = "Exit", command = root.destroy)
exit_button.place(x=1506, y=0)
frame = tk.Frame(root)
main_entry = tk.Entry(root, width = 100, fg = "black")
main_entry.place(x=50, y=50)
frame.place(relx=.5,rely=.5, anchor='center')
root.mainloop()
如您所见,框架未居中,我该如何解决?
【问题讨论】:
-
也许可以为背景添加颜色以查看它的外观。也许 Frame 居中,但 Entry 未居中。
-
Frame自动将大小更改为Frame内对象的大小(当您使用pack()时)-但您在Frame内没有任何内容-您将所有小部件直接放在root中-所以它没有大小(宽度为零,高度为零)并且不可见。当我使用tk.Frame(root, bg='red', width=100, height=100)时,我会在中心看到红框。顺便说一句:如果你想在Frame中使用Entry,那么使用tk.Entry(frame, ...)而不是tk.Entry(root, ...) -
所以你有两个问题:(1)你把
Entry放在错误的父级中-它必须是frame而不是root,(2)你使用place(),它没有' t 将Frame的大小调整为其子级 - 并且它的大小为零。
标签: python user-interface tkinter pycharm widget