Tkinter 使用 2d 数组作为条目的值

Tkinter using 2d array to as values for the entries

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

问:

我正在尝试创建一个程序,它询问用户矩阵的大小,然后创建一个给定大小的矩阵并创建另一组条目来显示解决方案。问题是如何使用二维数组的值作为解决方案矩阵的每个条目的值。我尝试使用.insert方法,但它只显示为空白。 我只是使用答案数组进行示例和测试。

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=380, y=150)

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

answerMatrix = tb.Frame(root)
answerMatrix.place(x=700, 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"]
answer = [[1,2],
          [3,4]]

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, answerMatrix))
# 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=250)

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 showAnswer(self, answerMatrix,answer):
        colSize = int(colCombo.get())
        rowSize = int(rowCombo.get())

        answer_widgets = []

        for i in range(rowSize):
            row_widgets = []
            for j in range(colSize):
                answer = Entry(answerMatrix, width=5, fg='blue', font=('Arial', 16, 'bold'))
                answer.grid(row=i, column=j)
                answer_widgets.append(answer)
                row_widgets.append(answer)

            answer_widgets.append(row_widgets)

                # answer.insert(END, str(answer[i][j]))  # You can set the initial value as needed
        for i in range(rowSize):
            for j in range(colSize):
                answer_widgets[i][j].insert(END, str(answer[i][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, answerMatrix):
    cols, rows = setupMatrix.grid_size()
    matrix_values = [
        [int(setupMatrix.grid_slaves(row=row, column=col)[0].get())  # Convert to int
         for col in range(cols)] for row in range(rows)
    ]
    table_instance.showAnswer(answerMatrix,answer)

    print(matrix_values)

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

root.mainloop()

蟒蛇 tkinter

评论

0赞 acw1668 11/17/2023
你用里面覆盖参数。另请注意,使用可能会有问题,因为这些组合框的选择可能会在单击按钮之后但在单击按钮之前更改。answeranswer = Entry(...)showAnswer()colSize = int(colCombo.get())Create matrixSolve Matrix
0赞 Bryan Oakley 11/17/2023
如果您可以减少代码量,那将会有所帮助。例如,ttkbootstrap 是否必须能够重现问题?

答:

0赞 acw1668 11/17/2023 #1

您已通过里面的行覆盖了参数。为条目小组件使用其他名称。answeranswer = Entry(...)showAnswer()

此外,如果在单击按钮之前更改了这些值,则使用内部组合框的当前值获取网格大小可能会引发异常。请改用参数来确定网格大小:showAnswer()Solve Matrixanswer

def showAnswer(self, answerMatrix, answer):
    #colSize = int(colCombo.get())
    #rowSize = int(rowCombo.get())
    # use argument "answer" to determine the grid size
    rowSize = len(answer)
    colSize = len(answer[0])

    answer_widgets = []

    for i in range(rowSize):
        row_widgets = []
        for j in range(colSize):
            # use other name instead of 'answer' fo the entry box
            entry = Entry(answerMatrix, width=5, fg='blue', font=('Arial', 16, 'bold'))
            entry.grid(row=i, column=j)
            #answer_widgets.append(answer) # should not call it here
            row_widgets.append(entry)

        answer_widgets.append(row_widgets)

    for i in range(rowSize):
        for j in range(colSize):
            answer_widgets[i][j].insert(END, str(answer[i][j]))