Python Tkinter 在 for 循环中创建的一系列 Entry 中获取值

Python Tkinter getting values in a series of Entries created in a for loop

提问人:Gabriele 提问时间:11/16/2023 更新时间:11/16/2023 访问量:27

问:

我正在尝试创建一个程序,用户通过组合框选择矩阵的大小,根据组合框的值创建多个条目,在创建的 Entry 属性中输入值。如何获取在 for 循环中创建的 Entry 的值,然后将它们存储在 2d 数组中。我尝试对此使用 winfo_children(),但它输出了一个空数组。

from tkinter import *
from ttkbootstrap.constants import *
import ttkbootstrap as tb
import os

os.environ['TK_SILENCE_DEPRECATION'] = '1'

root = tb.Window(themename="solar")  # dark mode
# root = tb.Window(themename="cerculean") # light mode

root.title("RREF CALCULATOR")

root.geometry('1000x800')
root.resizable(False, False)
mytitle = tb.Label(text="Find the RREF of a Matrix from 2x2 to 5x5", font=('helvetica', 29), bootstyle=DEFAULT)

setup = tb.Frame(root, bootstyle=DEFAULT)
setup.place(x=400, y=150)

setupMatrix = tb.Frame(root)
setupMatrix.place(x=150, y=450)

# labels for row and col,
row = tb.Label(setup, text="Enter number of Rows", font=('helvetica', 18), bootstyle=DEFAULT)
col = tb.Label(setup, text="Enter number of Columns", font=('helvetica', 18), bootstyle=DEFAULT)

matrixSize = ["2", "3", "4", "5"]

myButton = tb.Button(setup, text="Create matrix", bootstyle=DEFAULT, command=lambda: setCol(setupMatrix))
resetButton = tb.Button(setup, text="Reset", bootstyle=DEFAULT, command=lambda: resetMatrix(setupMatrix, rowCombo, colCombo))
solveButton = tb.Button(root, text="Solve Matrix", bootstyle=DEFAULT, command=lambda: solveMatrix(setupMatrix))
# dropdowns for the size of the matrix
colCombo = tb.Combobox(setup, bootstyle=DEFAULT, values=matrixSize)
colCombo.current(0)
rowCombo = tb.Combobox(setup, bootstyle=DEFAULT, values=matrixSize)
rowCombo.current(0)

row.pack(pady=5)
rowCombo.pack(pady=5)
col.pack(pady=5)
colCombo.pack(pady=10)
myButton.pack(side=LEFT)
resetButton.pack(side=RIGHT)
mytitle.pack(pady=50)
solveButton.pack(pady=310)

class Table:
    def setCol(self, setupMatrix):
        colSize = int(colCombo.get())
        rowSize = int(rowCombo.get())

        for i in range(rowSize):
            for j in range(colSize):
                entry = Entry(setupMatrix, width=5, fg='blue', font=('Arial', 16, 'bold'))
                entry.grid(row=i, column=j)

def setCol(setupMatrix):
    table_instance.setCol(setupMatrix)

def resetMatrix(setupMatrix, rowCombo, colCombo):
    for widget in setupMatrix.winfo_children():
        widget.destroy()
    rowCombo.set('')  # Clear the selected value in the combobox
    colCombo.set('')  # Clear the selected value in the combobox

def solveMatrix(setupMatrix):
    matrix_values = []
    for i in range(len(setupMatrix.winfo_children())):
        row_values = []
        for j in range(len(setupMatrix.winfo_children()[i].winfo_children())):
            entry = setupMatrix.winfo_children()[i].winfo_children()[j]
            value = entry.get()
            row_values.append(value)
        matrix_values.append(row_values)

    # Now matrix_values contains the values of the matrix in a 2D array
    print(matrix_values)

# Create an instance of the Table class
table_instance = Table()

root.mainloop()
蟒蛇 tkinter

评论


答:

1赞 acw1668 11/16/2023 #1

请注意,它会在小部件内返回其子数组的一维数组,并且可能不会按您想要的顺序返回。widget.winfo_children()

您可以使用以下命令创建二维数组

  • .grid_size()返回行数和列数
  • .grid_slave(row=row, column=col)在 (row, col) 处返回小部件
def solveMatrix(setupMatrix):
    cols, rows = setupMatrix.grid_size()
    matrix_values = [[setupMatrix.grid_slaves(row=row, column=col)[0].get()
                         for col in range(cols)] for row in range(rows)]
    # Now matrix_values contains the values of the matrix in a 2D array
    print(matrix_values)

另一种方法是在内部创建一个二维数组,并将这些输入框放入数组中。Table.__init__()