python-3.x 你能一次配置所有行/列的最小尺寸吗?

4zcjmb1e  于 2023-04-22  发布在  Python
关注(0)|答案(3)|浏览(120)

我有一个tkinter应用程序,我正在为一个笔记回忆工具工作。我的问题是在.grid布局。我想知道是否有一种方法可以使用.columnconfigure().rowconfigure()来配置所有行和所有列的最小大小,使用类似范围的东西来定义所有行/列。现在我必须为每一行和每一列分别定义minsize=。它只是接缝,就像应该有一个更简单的方法来定义所有的行/列一次。
这是我的代码

from tkinter import *
from tkinter.ttk import *
import tkinter.font
import subprocess as sub
import tkinter.messagebox

#~~~~~~~~~~~< spacer grid >~~~~~~~~~~~
spacerC0=Label(root, text="0").grid(row = 1, column = 0)
spacerC1=Label(root, text="1").grid(row = 1, column = 1)
spacerC2=Label(root, text="2").grid(row = 1, column = 2)
spacerC3=Label(root, text="3").grid(row = 1, column = 3)
spacerC4=Label(root, text="4").grid(row = 1, column = 4)
spacerC5=Label(root, text="5").grid(row = 1, column = 5)
spacerC6=Label(root, text="6").grid(row = 1, column = 6)
spacerC7=Label(root, text="7").grid(row = 1, column = 7)
spacerC8=Label(root, text="8").grid(row = 1, column = 8)
spacerC9=Label(root, text="9").grid(row = 1, column = 9)
root.columncel(root, text="0").grid(row = 1)
spacerR1=Label(root, text="1").grid(row = 2)
spacerR2=Label(root, text="2").grid(row = 3)
spacerR3=Label(root, text="3").grid(row = 4)
spacerR4=Label(root, text="4").grid(row = 5)
spacerR5=Label(root, text="5").grid(row = 6)
spacerR6=Label(root, text="6").grid(row = 7)
spacerR7=Label(root, text="7").grid(row = 8)
spacerR8=Label(root, text="8").grid(row = 9)
spacerR9=Label(root, text="9").grid(row = 10)
root.rowconfigure(0, minsize=60)
root.rowconfigure(1, minsize=60)
root.rowconfigure(2, minsize=60)
root.rowconfigure(3, minsize=60)
root.rowconfigure(4, minsize=60)
root.rowconfigure(5, minsize=60)
root.rowconfigure(6, minsize=60)
root.rowconfigure(7, minsize=60)
root.rowconfigure(8, minsize=60)
root.rowconfigure(9, minsize=60)
root.rowconfigure(10, minsize=60)
amrnrhlw

amrnrhlw1#

当然可以。

for row_num in range(root.grid_size()[1]):
    root.rowconfigure(row_num, minsize=60)

编辑:你可以将其与一个循环结合起来创建所有这些标签:

#~~~~~~~~~~~< spacer grid >~~~~~~~~~~~
for col_num in range(1, 10):
    spacer=Label(root, text=str(col_num))
    spacer.grid(row = 0, column = col_num)
for row_num in range(10):
    spacer=Label(root, text=str(row_num))
    spacer.grid(row = row_num, column = 0)
    root.rowconfigure(row_num, minsize=60)
wribegjk

wribegjk2#

我创建了一个相当小的(62行)DSL来布局类似于Flutter的TKinter网格。
示例:

from tkinter import W, Tk
from tkinter import ttk
from grid import Grid, Row, RowConfigure, ColumnConfigure, GridOptions, Dummy

window = Tk()
window.title("Grid Layout DSL")
window.geometry('400x400')

def on_click():
    print("Hello from Grid layout")

layout = Grid(
    parent = window,
    row_configure = RowConfigure(pad = 10),
    col_configure = ColumnConfigure(pad = 10),
    grid_options = GridOptions(sticky=W),
    rows = [
        Row(
            children=[
                ttk.Label(window ,text = "First Name:"), 
                ttk.Entry(window)]),
        Row(
            children=[
                ttk.Label(window ,text = "Last Name:"), 
                ttk.Entry(window)]),
        Row(
            children=[
                ttk.Label(window ,text = "Email:"), 
                ttk.Entry(window)]),
        Row(
            children=[
                ttk.Label(window ,text = "Number:"), 
                ttk.Entry(window)]),
        Row(
            children=[
                Dummy(window), 
                ttk.Button(window ,text="Submit", command=on_click)]),
    ]
)

layout.layout()
window.mainloop()

代码只是一个grid.py(62行)文件,可以在这里免费获得:https://gist.github.com/mnesarco/df03075d8806ed72d784ccabc2d52f12

5gfr0r5j

5gfr0r5j3#

我刚从一个真正的长途旅行回来。抱歉耽搁了。OP只要求rowconf部分。所以:
root.rowconfigure(list(range(11)), minsize=60)
正如@Novel在他的回答中指出的那样,我们可以通过抽象出硬编码的魔法“11”来使其更具动态性:
root.rowconfigure(list(range(root.grid_size()[1])), minsize=60)

相关问题