[英]Resizing Window in Tkinter
我试图搜索为什么我无法让内容保持居中,因为我将 window 调整得更大,但不太明白我做错了什么。
据我了解,将权重设置为 columnconfigure 和 rowconfigure 将缩放 window 的内容扩展程度,但目前 window 的扩大使内容保持在左上角,有人可以帮我理解我哪里出错了吗?
def calculate():
try:
numValue = 100*float(num.get())/8
num.set(str("%.2f"%numValue))
except ValueError:
num.set("Please enter a positive integer")
pass
num = StringVar()
num_entry = ttk.Entry(mainframe, width=20, textvariable=ozone)
num_entry.grid(column=2, row=1, sticky=(W, E))
num_entry.rowconfigure(0, weight=1)
num_entry.columnconfigure(0, weight=2)
ttk.Label(mainframe, text="Number:").grid(column=1, row=1, sticky=E)
ttk.Label(mainframe, textvariable=num).grid(column=2, row=7, sticky=(S))
ttk.Button(mainframe, width=20, text="Calculate Number", command=calculate).grid(column=2, row=5, sticky=W)
for child in mainframe.winfo_children():
child.grid_configure(padx=5, pady=10)
root.mainloop()
为您的框架和根设置rowconfigure
和columnconfigure
,而不是为条目和其他小部件设置。 您已经将小部件放入框架中,所以不要使用sticky
。
(...)
mainframe = Frame(root, width=500, height=500, background='green')
mainframe.grid(row=0, column=0)
mainframe.grid_rowconfigure(0, weight=1)
mainframe.grid_columnconfigure(0, weight=1)
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
num_entry = ttk.Entry(mainframe, width=20, textvariable='ozone')
num_entry.grid(column=2, row=1)
# num_entry.rowconfigure(0, weight=1) not like this
# num_entry.columnconfigure(0, weight=2)
(...)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.