Python Tkinter Grid无法正常工作

blpfk2vs  于 2023-05-21  发布在  Python
关注(0)|答案(3)|浏览(194)

我试图安排在顶部和小部件的标志3部分下面的标志。我试图将这3部分的比例设置为相同,但当我运行下面的代码时,比例不相同。
我的准则是

import tkinter as tk

window = tk.Tk()
window.title("My Program")
window.geometry("700x400")

logo = tk.Label(window, text="Logo")
logo.grid(row=0, column=0, columnspan=3, sticky="nw", padx=10, pady=10)

window.columnconfigure(0, weight=1)
window.columnconfigure(1, weight=1)
window.columnconfigure(2, weight=1)

window.rowconfigure(1, weight=1)
window.rowconfigure(2, weight=1)
window.rowconfigure(3, weight=1)

button1 = tk.Button(window, text="Button 1")
button1.grid(row=1, column=0, sticky="nsew", padx=10, pady=10)

button2 = tk.Button(window, text="Button 2")
button2.grid(row=2, column=0, sticky="nsew", padx=10, pady=10)

button3 = tk.Button(window, text="Button 3")
button3.grid(row=3, column=0, sticky="nsew", padx=10, pady=10)

big_label = tk.Label(window, text="Big Label")
big_label.grid(row=1, column=1, rowspan=3, sticky="nsew", padx=10, pady=10)

button4 = tk.Button(window, text="Button 4")
button4.grid(row=1, column=2, sticky="nsew", padx=10, pady=10)

button5 = tk.Button(window, text="Button 5")
button5.grid(row=2, column=2, sticky="nsew", padx=10, pady=10)

entry = tk.Entry(window)
entry.grid(row=3, column=2, sticky="nsew", padx=10, pady=10)

window.mainloop()

运行代码时,结果低于

正如你所看到的,第二列比其他的要厚。我该如何解决这个问题?

f5emj3cl

f5emj3cl1#

如果你想让所有的列都有相同的大小,你需要在所有的.columnconfigure(...)中设置uniform=1

window.columnconfigure(0, weight=1, uniform=1)
window.columnconfigure(1, weight=1, uniform=1)
window.columnconfigure(2, weight=1, uniform=1)

对于所有.rowconfigure(...)均相同。
结果:

wn9m85ua

wn9m85ua2#

tkinter小部件有参数,比如width和height。下面是一个例子:

button = tk.Button(window, width=20)

对于网格,您必须使用网格列配置并将其设置为不同的权重。

window.columnconfigure(0, weight=1)

对于配置行来说是相同的。

dzhpxtsq

dzhpxtsq3#

要在Tkinter代码中实现徽标下面三个部分的相等比例,可以在grid_rowconfigure()函数中使用weight参数。下面是代码的更新版本,其中包含了为实现相等比率而进行的修改:

import tkinter as tk
window = tk.Tk()
window.title("My Program")
window.geometry("700x400")

logo = tk.Label(window, text="Logo")
logo.grid(row=0, column=0, columnspan=3, sticky="nw", padx=10, 
pady=10)

window.columnconfigure(0, weight=1)
window.columnconfigure(1, weight=1)
window.columnconfigure(2, weight=1)

window.rowconfigure(1, weight=1)
window.rowconfigure(2, weight=1)
window.rowconfigure(3, weight=1)

button1 = tk.Button(window, text="Button 1")
button1.grid(row=1, column=0, sticky="nsew", padx=10, pady=10)

button2 = tk.Button(window, text="Button 2")
button2.grid(row=2, column=0, sticky="nsew", padx=10, pady=10)

button3 = tk.Button(window, text="Button 3")
button3.grid(row=3, column=0, sticky="nsew", padx=10, pady=10)

big_label = tk.Label(window, text="Big Label")
big_label.grid(row=1, column=1, rowspan=3, sticky="nsew", 
padx=10, pady=10)

button4 = tk.Button(window, text="Button 4")
button4.grid(row=1, column=2, sticky="nsew", padx=10, pady=10)

button5 = tk.Button(window, text="Button 5")
button5.grid(row=2, column=2, sticky="nsew", padx=10, pady=10)

# Add weight to the rows to achieve equal ratios
window.rowconfigure(0, weight=0)  # No weight for the logo row
window.rowconfigure(1, weight=1)
window.rowconfigure(2, weight=1)
window.rowconfigure(3, weight=1)

window.mainloop()

相关问题