如何在不对齐按钮或修复对齐方式的情况下缩放按钮?
How to scale buttons without misaligning them or fix the alignment?
我一直在努力学习如何使用 python 和 Tkinter。
当我有多列不同尺寸的按钮时,它们不会像我期望的那样根据我制作的尺寸对齐。
外观:
如您所见,高度设置为 100 的按钮仅使用与 6 个高度为 10 的按钮相同的 space,而不是像预期的那样占据整个高度。
预期结果:
有什么方法可以按预期对齐按钮,而无需手动尝试找出点亮它们所需的尺寸?
代码:
import tkinter as tk
import tkinter.font as tkFont
root = tk.Tk()
pixelVirtual = tk.PhotoImage(width=1, height=1)
large = tk.Button(root,
text='Height=100',
image=pixelVirtual,
width=100,
height=100,
compound='c')
for i in range(10):
small = tk.Button(root,
text='Height=10',
image=pixelVirtual,
width=100,
height=10,
compound='c')
small.grid(row=i, column= 1)
large.grid(row=0, rowspan=10, column=0)
root.mainloop()
因为small
按钮的最终高度不是10,我的系统是18:
border width
是2 x 2(上下),即4
highlightthickness
是1 x 2(上下),即2
pady
是1 x 2(上下),即2
- text/image面积为10
总计 4 + 2 + 2 + 10 = 18
类似于 large
按钮:4 + 2 + 2 + 100 = 108
即 6 x 18
。
为了对齐large
按钮,在grid(...)
中添加sticky='nsew'
:
large.grid(row=0, rowspan=10, column=0, sticky='nsew')
我一直在努力学习如何使用 python 和 Tkinter。 当我有多列不同尺寸的按钮时,它们不会像我期望的那样根据我制作的尺寸对齐。
外观:
如您所见,高度设置为 100 的按钮仅使用与 6 个高度为 10 的按钮相同的 space,而不是像预期的那样占据整个高度。
预期结果:
有什么方法可以按预期对齐按钮,而无需手动尝试找出点亮它们所需的尺寸?
代码:
import tkinter as tk
import tkinter.font as tkFont
root = tk.Tk()
pixelVirtual = tk.PhotoImage(width=1, height=1)
large = tk.Button(root,
text='Height=100',
image=pixelVirtual,
width=100,
height=100,
compound='c')
for i in range(10):
small = tk.Button(root,
text='Height=10',
image=pixelVirtual,
width=100,
height=10,
compound='c')
small.grid(row=i, column= 1)
large.grid(row=0, rowspan=10, column=0)
root.mainloop()
因为small
按钮的最终高度不是10,我的系统是18:
border width
是2 x 2(上下),即4highlightthickness
是1 x 2(上下),即2pady
是1 x 2(上下),即2- text/image面积为10
总计 4 + 2 + 2 + 10 = 18
类似于 large
按钮:4 + 2 + 2 + 100 = 108
即 6 x 18
。
为了对齐large
按钮,在grid(...)
中添加sticky='nsew'
:
large.grid(row=0, rowspan=10, column=0, sticky='nsew')