问题描述
我试图创建按钮的网格(以实现可点击的细胞效应)与Tkinter的。
我的主要问题是,我不能让电网
和按钮自动调整和适应的父窗口。
例如,当我对电网大量的按钮,而不是使电网的窗口内符合萎缩的按钮,我得到拉伸帧熄灭屏幕。
这是我期待的效果是网格填充所有可用空间,然后调整其细胞,以适应该空间内。我在文件读过,但我仍然无法弄清楚如何使它发挥作用。
这是基本的code这是我的出发点:
高清__init __(个体经营):
根= TK()
帧=帧(根)
frame.grid() #some部件得到的前6行帧的格中加入 #initialize格
格=帧(帧)
grid.grid(粘= N + S + E + W,列= 0,行= 7,columnspan = 2) #示例值
在范围X(60):
y的范围内(30):
BTN =按钮(网格)
btn.grid(列= X,排= Y) root.mainloop()
您需要配置的行和列有非零重量,以便他们会占用额外的空间:
在范围X(60):
Grid.columnconfigure(格中,x,重量= 1)y的范围内(30):
Grid.rowconfigure(网格,Y,重量= 1)
您还需要配置你的按钮,使他们的扩展以填充细胞
btn.grid(列= X,排= Y,粘= N + S + E + W)
这必须做一路上涨,所以这里是一个完整的例子:
从Tkinter的进口*根= TK()
帧=帧(根)
Grid.rowconfigure(根,0,重量= 1)
Grid.columnconfigure(根,0,重量= 1)
frame.grid(行= 0,列= 0,粘= N + S + E + W)
格=帧(帧)
grid.grid(粘= N + S + E + W,列= 0,行= 7,columnspan = 2)
Grid.rowconfigure(帧,7,重量= 1)
Grid.columnconfigure(帧,0,重量= 1)#示例值
对于在范围X(10):
对于在范围Y(5):
BTN =按钮(帧)
btn.grid(列= X,排= Y,粘= N + S + E + W)对于在范围X(10):
Grid.columnconfigure(帧中,x,重量= 1)对于在范围Y(5):
Grid.rowconfigure(帧,Y,重量= 1)root.mainloop()
I am trying to create a grid of buttons(in order to achieve the clickable cell effect) with Tkinter.
My main problem is that I cannot make the grid
and the buttons autoresize and fit the parent window.
For example, when I have a high number of buttons on the grid, instead of shrinking the buttons so that the grid fits inside the window, I get a stretched frame that goes off screen.
The effect that I am looking for is the grid filling all available space, then resizing its cells to fit within that space. I have read at the documentation, but I still cannot figure out how to make it work.
This is the basic code which is my starting point:
def __init__(self):
root = Tk()
frame = Frame(root)
frame.grid()
#some widgets get added in the first 6 rows of the frame's grid
#initialize grid
grid = Frame(frame)
grid.grid(sticky=N+S+E+W, column=0, row=7, columnspan=2)
#example values
for x in range(60):
for y in range(30):
btn = Button(grid)
btn.grid(column=x, row=y)
root.mainloop()
You need to configure the rows and columns to have a non-zero weight so that they will take up the extra space:
for x in range(60):
Grid.columnconfigure(grid, x, weight=1)
for y in range(30):
Grid.rowconfigure(grid, y, weight=1)
You also need to configure your buttons so that they will expand to fill the cell:
btn.grid(column=x, row=y, sticky=N+S+E+W)
This has to be done all the way up, so here is a full example:
from tkinter import *
root = Tk()
frame=Frame(root)
Grid.rowconfigure(root, 0, weight=1)
Grid.columnconfigure(root, 0, weight=1)
frame.grid(row=0, column=0, sticky=N+S+E+W)
grid=Frame(frame)
grid.grid(sticky=N+S+E+W, column=0, row=7, columnspan=2)
Grid.rowconfigure(frame, 7, weight=1)
Grid.columnconfigure(frame, 0, weight=1)
#example values
for x in range(10):
for y in range(5):
btn = Button(frame)
btn.grid(column=x, row=y, sticky=N+S+E+W)
for x in range(10):
Grid.columnconfigure(frame, x, weight=1)
for y in range(5):
Grid.rowconfigure(frame, y, weight=1)
root.mainloop()
这篇关于如何创建Tkinter的按键的自设网格尺寸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!