【问题标题】:Tkinter - Change Button Position on expanding window sizeTkinter - 在扩展窗口大小时更改按钮位置
【发布时间】:2020-09-01 15:21:07
【问题描述】:

我的预期输出是:

 _______________________________
|-------------------------------|
|            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://i.stack.imgur.com/ItxvB.jpg

预期输出:即使在展开的窗口中(编辑后的图像) https://i.stack.imgur.com/Koe34.jpg

【问题讨论】:

  • 尝试将place()relx=0.25relx=0.5relx=0.75 分别用于3 个按钮,将rely=0.5 用于所有3 个按钮。

标签: python button tkinter grid pack


【解决方案1】:

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

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

我从代码中删除了 frame2,只使用了一帧(“帧”)作为容器。 在上面的图片和下面的代码中你会看到:

  • 我将导入修改为不使用 *(我读过这不是一个好习惯)。 所以tk. 根据需要添加了
  • 标签“示例”位于第 0 行第 0 列,跨越 3 个单元格
  • 标签“label2”位于第 1 行第 3 列
  • 我在按钮 3 上方的第 1 行第 2 列创建了另一个标签 (label1)(我很困惑,因为您链接的图像 [预期输出] 与您在问题开始时提供的草图不同
  • 3 个按钮分别位于第 2 行、第 0、1 和 2 列中
  • 我为按钮添加了一个 pady=40 以获得更好的外观
  • rowconfigure 和 columnconfigure 需要与 weight 选项一起使用,以便调整网格的大小

HTH

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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-11
    • 1970-01-01
    • 1970-01-01
    • 2015-04-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多