如何使Tkinter按钮小部件占据网格的整个宽度 [英] How to make tkinter button widget take up full width of grid
本文介绍了如何使Tkinter按钮小部件占据网格的整个宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试了
我希望盒子都一样宽,并填满可用空间.结果应类似于以下内容:
如何使按钮占据行/列的整个宽度?
解决方案
两件事:
- 您将输入框设置为从第0列开始应用,但随后的每一行都从第1列开始进行操作.请保持一致-您的7个按钮应位于0列,8合1列中,等等.
- 当您按下
.grid
按钮时,请使用sticky = N + S + E + W
.这将允许按钮以其各自的行和列大小展开.
更新:N + S + E + W不适用于python3.6.7,它是 nesw
或这4个字母的任意组合.
例如:
#!/usr/bin/python#-*-编码:utf-8-*-从tkinter导入*窗口= Tk()disp =条目(窗口,状态=只读",readonlybackground =白色")disp.grid(column = 0,row = 0,columnpan = 4)#行1七个=按钮(窗口,文本="7")seven.grid(column = 0,row = 1,sticky ='nesw')八个=按钮(窗口,文本="8")8.grid(column = 1,row = 1,sticky ='nesw')九=按钮(窗口,文本="9")nine.grid(列= 2,行= 1,粘性='nesw')除法=按钮(窗口,文字=÷")divid.grid(列= 3,行= 1,粘性='nesw')window.mainloop()
返回一个看起来像这样的窗口
I've tried this but it didn't help.
I'm making a calculator program. I've made this so far:
from tkinter import *
window = Tk()
disp = Entry(window, state='readonly', readonlybackground="white")
disp.grid(column=0, row=0, columnspan=4)
#row 1
seven = Button(window, text="7", command=Seven)
seven.grid(column=1,row=1)
eight = Button(window, text="8", command=Eight)
eight.grid(column=2,row=1)
nine = Button(window, text="9", command=Nine)
nine.grid(column=3,row=1)
divide = Button(window, text="÷", command=Divide)
divide.grid(column=4,row=1)
#row 2
four = Button(window, text="4", command=Four)
four.grid(column=1,row=2)
five = Button(window, text="5", command=Five)
five.grid(column=2,row=2)
six = Button(window, text="6", command=Six)
six.grid(column=3,row=2)
multiply = Button(window, text="×", command=Multiply)
multiply.grid(column=4,row=2)
#row 3
one = Button(window, text="1", command=One)
one.grid(column=1,row=3)
two = Button(window, text="2", command=Two)
two.grid(column=2,row=3)
three = Button(window, text="3", command=Three)
three.grid(column=3,row=3)
minus = Button(window, text="-", command=Minus)
minus.grid(column=4,row=3)
#row 4
zero = Button(window, text="0", command=Zero)
zero.grid(column=1,row=4)
dec = Button(window, text=".", command=Dec)
dec.grid(column=2,row=4)
equal = Button(window, text="=", command=Equal)
equal.grid(column=3,row=4)
add = Button(window, text="+", command=Add)
add.grid(column=4,row=4)
window.mainloop()
This looks like this:
I'd like the boxes to be equally wide and fill the available space.
The result should look similar to this:
How do you make a button take up the entire width of the row/column?
解决方案
Two things:
- You set your entry box to apply from column 0 onwards, but then each subsequent row operates from column 1 onwards. Be consistent in this - your button for 7 should be in column 0, 8 in 1 etc.
- When you
.grid
your buttons, usesticky=N+S+E+W
. This will allow the buttons to expand with their respective row and column sizes.
Update:
N+S+E+W is not working for python3.6.7 it is nesw
or any combination of these 4 letters.
For example:
#!/usr/bin/python
# -*- coding: utf-8 -*-
from tkinter import *
window = Tk()
disp = Entry(window, state='readonly', readonlybackground="white")
disp.grid(column=0, row=0, columnspan=4)
#row 1
seven = Button(window, text="7")
seven.grid(column=0,row=1, sticky='nesw')
eight = Button(window, text="8")
eight.grid(column=1,row=1, sticky='nesw')
nine = Button(window, text="9")
nine.grid(column=2,row=1, sticky='nesw')
divide = Button(window, text="÷")
divide.grid(column=3,row=1, sticky='nesw')
window.mainloop()
returns a window that looks like:
这篇关于如何使Tkinter按钮小部件占据网格的整个宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文