如何使用 canvas 使所有按钮根据 tkinter 中最大的按钮自动变为相同大小?
How to get all the buttons to be automatically the same size depending on the largest one in tkinter using canvas?
无论您在其中插入的文本如何,您应该如何将所有按钮的大小调整为相同的大小,它们都应该根据最大的按钮调整大小。
已经有一个类似我的问题的答案,但它是使用 grid
完成的,我正在使用 canvas 在 window 中放置背景图像并放置按钮。
是否值得根据文本自动将按钮设置为相同大小,因为我的文本将始终大致相同...
我尝试使用 cget()
获取按钮的大小,但是 returns 0。它在哪里存储它的宽度,因为它必须以某种方式调整自己的大小,即使它根据文本进行调整?可以以任何方式访问它吗?我想用那个值来调整其他按钮的值,但结果失败了。
如果你想知道为什么我要把它做成 class,我也想试试。
我通过将所有按钮放在一个框架中并告诉它们 fill=x
来让它工作,但是使用框架破坏了使用 canvas 的意义,因为无法看到背景,因为框架覆盖它。有没有办法让 canvas 中的框架透明,这也可能解决我的问题。
from tkinter import *
class ThreeButtonMenu():
def __init__(self, button1_text, button2_text, button3_text, image_height = 600, image_width = 500, bg_input = 'space_background.png'):
self.root = Tk()
HxW = str(image_height)+'x'+str(image_width)
self.root.geometry(HxW)
self.root.maxsize(image_height,image_width)
self.root.minsize(image_height,image_width)
self.root.title('Guess')
bg = PhotoImage(file=bg_input)
background_canvas = Canvas(self.root, width = 600, height=500)
background_canvas.pack(fill="both", expand=True)
background_canvas.create_image(0,0, image=bg, anchor='nw')
button1 = Button(self.root, text=button1_text, font = ('Lato',28))
button2 = Button(self.root, text=button2_text, font = ('Lato',28))
button3 = Button(self.root, text=button3_text, font = ('Lato',28), command = self.root.destroy)
button1_window = background_canvas.create_window(300,45, anchor=N, window=button1)
button2_window = background_canvas.create_window(300,160, anchor=N, window=button2)
button3_window = background_canvas.create_window(300,275, anchor=N, window=button3)
print(button1.cget('width'))
print(button2.cget('width'))
print(button3.cget('width'))
self.root.mainloop()
start_menu = ThreeButtonMenu('Start Game', 'Leaderboard', 'Quit')
感谢您的回答。
您通常会在使用几何管理器时执行此操作(pack
、place
或 grid
)。
例如,您需要在每个按钮上调用 pack
。请参阅下面的 pack
示例。
import tkinter as tk
root = tk.Tk()
for text in (
"Hello", "short", "All the buttons are not the same size",
"Options", "Test2", "ABC", "This button is so much larger"):
button = tk.Button(root, text=text)
button.pack(side="top", fill="x")
root.mainloop()
无论您在其中插入的文本如何,您应该如何将所有按钮的大小调整为相同的大小,它们都应该根据最大的按钮调整大小。
已经有一个类似我的问题的答案,但它是使用 grid
完成的,我正在使用 canvas 在 window 中放置背景图像并放置按钮。
是否值得根据文本自动将按钮设置为相同大小,因为我的文本将始终大致相同...
我尝试使用 cget()
获取按钮的大小,但是 returns 0。它在哪里存储它的宽度,因为它必须以某种方式调整自己的大小,即使它根据文本进行调整?可以以任何方式访问它吗?我想用那个值来调整其他按钮的值,但结果失败了。
如果你想知道为什么我要把它做成 class,我也想试试。
我通过将所有按钮放在一个框架中并告诉它们 fill=x
来让它工作,但是使用框架破坏了使用 canvas 的意义,因为无法看到背景,因为框架覆盖它。有没有办法让 canvas 中的框架透明,这也可能解决我的问题。
from tkinter import *
class ThreeButtonMenu():
def __init__(self, button1_text, button2_text, button3_text, image_height = 600, image_width = 500, bg_input = 'space_background.png'):
self.root = Tk()
HxW = str(image_height)+'x'+str(image_width)
self.root.geometry(HxW)
self.root.maxsize(image_height,image_width)
self.root.minsize(image_height,image_width)
self.root.title('Guess')
bg = PhotoImage(file=bg_input)
background_canvas = Canvas(self.root, width = 600, height=500)
background_canvas.pack(fill="both", expand=True)
background_canvas.create_image(0,0, image=bg, anchor='nw')
button1 = Button(self.root, text=button1_text, font = ('Lato',28))
button2 = Button(self.root, text=button2_text, font = ('Lato',28))
button3 = Button(self.root, text=button3_text, font = ('Lato',28), command = self.root.destroy)
button1_window = background_canvas.create_window(300,45, anchor=N, window=button1)
button2_window = background_canvas.create_window(300,160, anchor=N, window=button2)
button3_window = background_canvas.create_window(300,275, anchor=N, window=button3)
print(button1.cget('width'))
print(button2.cget('width'))
print(button3.cget('width'))
self.root.mainloop()
start_menu = ThreeButtonMenu('Start Game', 'Leaderboard', 'Quit')
感谢您的回答。
您通常会在使用几何管理器时执行此操作(pack
、place
或 grid
)。
例如,您需要在每个按钮上调用 pack
。请参阅下面的 pack
示例。
import tkinter as tk
root = tk.Tk()
for text in (
"Hello", "short", "All the buttons are not the same size",
"Options", "Test2", "ABC", "This button is so much larger"):
button = tk.Button(root, text=text)
button.pack(side="top", fill="x")
root.mainloop()