Tkinter - 如何在窗口大小改变时改变按钮位置

3

我的期望输出是:

 _______________________________
|-------------------------------|
|            EXAMPLE            |
|                       Label1  |
| ______     _______    ______  |
||      |   |       |  |      | |
||      |   |       |  |      | |
||______|   |_______|  |______| |
|_______________________________|

我想要把这三个按钮等距离地放在中心位置,也就是说按钮的大小是固定的,如果用户试图扩大窗口的大小,则按钮之间的距离比应该保持不变。

以下是我的代码:

from tkinter import *

window = Tk()
window.minsize(710, 500)
window.state('zoomed')
window.title('Example')

frame = Frame(window)
frame.pack(fill=BOTH, expand=True)

frame2 = Frame(window)
frame2.pack(fill=BOTH, expand=True)


Label(frame, text="Example", fg='red3',
      font=('Eras Bold ITC', '65', 'bold')).pack(anchor = 'n', pady = 50)

Label(frame, text="Label2", fg='blue',
      font=('Calibri', '25', 'bold')).pack(anchor = 'e', padx = 40)

Button(frame2, height='10', width='20', text = 'image1').grid(row = 0, column = 0, padx = 20)
Button(frame2, height='10', width='20', text = 'image2').grid(row = 0, column = 6, padx = 20)
Button(frame2, height='10', width='20', text = 'image3').grid(row = 0, column = 12, padx = 20)

mainloop()

我使用了网格方法,但尝试使用pack()时,它会将按钮放置在第一个按钮的下方。 输出: https://istack.dev59.com/ItxvB.webp 期望输出: 在扩展窗口上也可以显示正常 (编辑后的图片) https://istack.dev59.com/Koe34.webp

尝试分别使用relx=0.25relx=0.5relx=0.75来设置3个按钮的位置,并将rely=0.5应用于所有3个按钮。 - acw1668
1个回答

1
这是一种在网格中组织小部件的方法。

https://i.postimg.cc/yYN0FX3D/tkinter-change-button-position-on-expanding-window-size.png

我从代码中删除了frame2,并只使用一个框架("frame")作为容器。 在上面的图片和下面的代码中,您将看到:
  • 我修改了导入方式,不再使用*(据说这不是一个好习惯)所以需要添加tk.
  • 标签“Example”放置在第0行、第0列,并跨越3个单元格
  • 标签“label2”放置在第1行、第3列
  • 我在第1行、第2列创建了另一个标签(label1),就在button3的上方(我有点混淆了,因为您链接的图片[预期输出]与您在问题开头呈现的草图不同)
  • 这3个按钮位于第2行,分别在第0、1和2列
  • 我为按钮添加了pady=40,以获得更好的外观
  • 需要使用weight选项设置rowconfigure和columnconfigure,以便网格可以调整大小
希望对您有所帮助。
import tkinter as tk

window = tk.Tk()
window.minsize(710, 500)
window.state('zoomed')
window.title('Example')

# the frame will be the main container, it's a child of the root window
frame = tk.Frame(window)
frame.grid(row=0, column=0, stick='news')
# tell our root window that its grid should be stretchable
window.rowconfigure(0, weight=1)
window.columnconfigure(0, weight=1)

# create a label, child of the frame, in row 0, column 0. It wiil span for 3 cells
tk.Label(frame, text="Example", fg='red3',
      font=('Eras Bold ITC', '65', 'bold')).grid(row=0, column=0, columnspan=3)

# create another label, child of the frame, in row 1, column 2
tk.Label(frame, text="Label1", fg='blue',
      font=('Calibri', '25', 'bold')).grid(row=1, column=2)


# create another label, child of the frame, in row 1, column 3
tk.Label(frame, text="Label2", fg='blue',
      font=('Calibri', '25', 'bold')).grid(row=1, column=3)

# create a button, child of the frame, in row 2, column 0
tk.Button(frame, height='10', width='20', text = 'image1').grid(row = 2, column = 0, padx = 20, pady=40)
# create a button, child of the frame, in row 2, column 1
tk.Button(frame, height='10', width='20', text = 'image2').grid(row = 2, column = 1, padx = 20, pady=40)
# create a button, child of the frame, in row 2, column 2
tk.Button(frame, height='10', width='20', text = 'image3').grid(row = 2, column = 2, padx = 20, pady=40)

# tell our frame that its grid should resize (stretchable) with same ratio for row heights and column widths
frame.rowconfigure(0, weight=1)
frame.rowconfigure(1, weight=1)
frame.columnconfigure(0, weight=1)
frame.columnconfigure(1, weight=1)
frame.columnconfigure(2, weight=1)
frame.columnconfigure(3, weight=1)

window.mainloop()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接