【问题标题】:Displaying square Tkinter.Button's?显示方形 Tkinter.Button 的?
【发布时间】:2014-07-05 17:24:53
【问题描述】:

为什么此代码显示按钮的高度大于宽度?

import Tkinter, tkFont
top = Tkinter.Tk()
right = Tkinter.Frame(top)
right.pack(side = "right")
font = tkFont.Font(family="Helvetica", size=60, weight = tkFont.BOLD)

for i in xrange(6):
    b = Tkinter.Button(right, text = str(i), font = font, width = 1, height = 1)
    top.rowconfigure(i, weight = 1)
    top.columnconfigure(i, weight = 1)
    b.grid(row = i/3, column = i%3, sticky = "NWSE")

top.mainloop()
  • 所有按钮都是用width=1, height=1创建的
  • 对于right 的每一行和每一列,都会调用right.rowconfigure(rowi, weight=1)(或columnconfigure)。
  • 每个按钮b 的每个网格位置都是用粘性NSEW 制作的。
  • 我已设置right.grid_propagate(0)

我到底做错了什么?

如果我将按钮直接放在top 上,按钮就会变得比高更宽。似乎它们正在调整大小以适应传播的空间。如何防止这种调整大小?

【问题讨论】:

  • 如果你希望你的按钮是 1px x 1px 或许可以考虑使用画布?
  • 我不希望我的按钮是 1 像素乘 1 像素,如果这就是你的意思。我只希望我的按钮是正方形的,不管里面是什么。目前,它们只有在文本足够小的情况下才是正方形,否则它们会变得更高。

标签: python tkinter


【解决方案1】:

如果Button 显示文本,当您使用heightwidth 选项时,它们的单位为文本单位。为了使它们成为正方形,使用像素单位会更好。为此,您需要将该按钮放在Frame 中并确保框架不会传播(grid_propagate)并允许其子级填充它(columnconfigurerowconfigure)。

这只是一个例子,因为我没有看到你的代码。

import Tkinter as tk

master = tk.Tk()

frame = tk.Frame(master, width=40, height=40) #their units in pixels
button1 = tk.Button(frame, text="btn")


frame.grid_propagate(False) #disables resizing of frame
frame.columnconfigure(0, weight=1) #enables button to fill frame
frame.rowconfigure(0,weight=1) #any positive number would do the trick

frame.grid(row=0, column=1) #put frame where the button should be
button1.grid(sticky="wens") #makes the button expand

tk.mainloop()

编辑:我刚刚看到您的编辑(添加您的代码)。将相同的东西应用到您的代码之后;

import Tkinter, tkFont
top = Tkinter.Tk()
right = Tkinter.Frame(top)
right.pack(side = "right")
font = tkFont.Font(family="Helvetica", size=20, weight = tkFont.BOLD)

for i in xrange(6):
    f = Tkinter.Frame(right,width=50,height=50)
    b = Tkinter.Button(f, text = str(i), font = font)

    f.rowconfigure(0, weight = 1)
    f.columnconfigure(0, weight = 1)
    f.grid_propagate(0)

    f.grid(row = i/3, column = i%3)
    b.grid(sticky = "NWSE")

top.mainloop()

【讨论】:

    【解决方案2】:

    另一种方法是诱使Button 认为它正在显示图像,因此它的单位可以通过像素而不是文本大小来操作。

    您可以通过创建PhotoImage 的空实例,将其设置为按钮的image 选项,并使用按钮的compound 选项来组合“图像”和文本来做到这一点。这是一个使用部分代码的示例:

    import Tkinter, tkFont
    
    
    root = Tkinter.Tk()
    
    font = tkFont.Font(family="Helvetica", size=60, weight = tkFont.BOLD)
    blank_image = Tkinter.PhotoImage()
    
    for i in xrange(6):
        b = Tkinter.Button(root, image=blank_image, text=str(i),
                           font=font, compound=Tkinter.CENTER)
    
        # get the height of the font to use as the square size
        square_size = font.metrics('linespace')
        b.config(width=square_size, height=square_size)
    
        b.grid(row = i/3, column = i%3, sticky = "NWSE")
    
    root.mainloop()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-10
      • 1970-01-01
      • 2011-06-14
      • 1970-01-01
      • 2022-07-13
      • 2021-08-22
      • 2014-02-14
      相关资源
      最近更新 更多