显示方形 Tkinter.Button 的? [英] Displaying square Tkinter.Button's?

查看:29
本文介绍了显示方形 Tkinter.Button 的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Why does this code display buttons taller than they are wide?

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)
    • All the buttons are created with width=1, height=1
    • For every row and every column of right there is a call to right.rowconfigure(rowi, weight=1) (or to columnconfigure).
    • Every grid placement of every button b is made with sticky NSEW.
    • I have set right.grid_propagate(0)
    • 我到底做错了什么?

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

      If I put the buttons directly onto top, the buttons just become wider than tall. It seems that they are being resized to accomodate propagated space. How do I prevent this resizing?

      推荐答案

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

      If a Button displays text, when you use height and width options, their units in text unit. To make them square, using pixel unit would be better. To do that, you need to place that button in a Frame and make sure frame won't propagate(grid_propagate) and allow its children to fill it (columnconfigure & rowconfigure).

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

      This is just an example, since I don't see your code.

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

      我刚刚看到您的编辑(添加您的代码).将相同的内容应用于您的代码后;

      I just saw your edit(adding your code). After applying same things to your code;

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

      这篇关于显示方形 Tkinter.Button 的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆