简体   繁体   English

避免使用Tkinter OptionMenu按钮调整大小

[英]Avoiding Tkinter OptionMenu button resizing

I've the following OptionMenu: 我有以下OptionMenu:

self.textoprioridad = Label(self.frameTwo, text = "Prioridad: ", justify="center")
self.textoprioridad.grid(row=n, column=4)
var2 = StringVar()
menu2 = OptionMenu(self.frameTwo, var2, "Primera pieza", "Esta semana", "Normal", "Baja")
menu2.grid(row=n, column=5, ipadx=10)
var2.set("Primera pieza")
self.optionmenus_prioridad.append((menu2, var2))

That shows something like this: 这显示了这样的事情:

在此输入图像描述

The thing is that if I choose Normal from the list, the button resized and it makes smaller: 问题是,如果我从列表中选择“ Normal ”,则按钮会调整大小并使其变小: 在此输入图像描述

I would like to know if it's any way to keep the OptionMenu button with the initial size, like this: 我想知道是否有任何方法可以保持OptionMenu按钮的初始大小,如下所示: 在此输入图像描述

Thanks in advance. 提前致谢。

通过config(width=desired-width)指定宽度config(width=desired-width)

menu2.config(width=20)

One solution is to give the widget a specific size by specifying the width attribute: 一种解决方案是通过指定width属性为窗口小部件指定特定大小:

menu2.configure(width=20)

Another solution is to have the widgets "stick" to the sides of their container: 另一个解决方案是让小部件“粘在”其容器的两侧:

menu2.grid(row=n, column=5, ipadx=10, sticky="ew")

Using the second option, the widgets can still possibly resize, but they are constrained by the size of the column they are in. If one resizes they all resize, guaranteeing they will always be uniform in size. 使用第二个选项,小部件仍然可以调整大小,但它们受到它们所在列的大小的限制。如果调整大小,它们都会调整大小,保证它们的大小始终是一致的。

Specify a sticky in the grid method so that it knows how wide to needs to be: grid方法中指定一个sticky ,以便它知道需要多宽:

button.grid(sticky="WE")

This method is better than specifying a set width in the grid method becuase it means that the widget will always span the length of the column. 此方法优于在grid方法中指定设置width ,因为它意味着窗口小部件将始终跨越列的长度。

sticky is a key that shows where the widget will stick too. sticky是一个键,可以显示小部件的位置。 if you say sticky='W' than the widget will stick to the left of the column. 如果你说sticky='W' ,那么小部件会粘在列的左边。 By making is EW it will stick to the left and right, and therefore stretch it's self to the length of the column. 通过制作是EW ,它将向左和向右伸展,因此将其自身伸展到柱的长度。 NOTE: it's North South East West 注意:它是北东南西

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM