如何在Tkinter中显示线性系统求解器的运算步骤?
Tkinter线性系统求解器:运算步骤Label无法正常显示问题
我正在开发基于Tkinter的线性系统求解器,程序支持输入变量数量、生成增广矩阵,最终输出解和运算步骤。但通过for循环添加Label组件展示运算步骤时,步骤无法正常显示。
原代码
from tkinter import Tk, Label, StringVar, Button, Entry, IntVar from fractions import Fraction from array import array import numpy as np from math import * from fractions import Fraction import termcolor # Define Programe window = Tk() window.title("Linear System Solver") window.geometry("640x500+120+120") # window.configure(bg='Black') # Function To get matrix From entry Box Label( window, text="Enter The Number Of Variables :", font=("arial", 10, "bold"), bg="Yellow", ).place(x=20, y=5) ent = Entry(window, textvariable=IntVar()) ent.place(x=235, y=7) # ent.pack() vars = int(ent.get()) text_var = [] entries = [] x = np.zeros(vars) #x = np.zeros(vars) Label(window, text="Enter The Augmented Matrix :", font=("arial", 10, "bold"), bg="Yellow").place( x=20, y=80 ) # Check if matrix[i][i]==0 ==> if That Sawp def Make_gui(): x2 = 0 y2 = 0 vars = int(ent.get()) for i in range(vars): # append an empty list to your two arrays # so you can append to those later text_var.append([]) entries.append([]) for j in range(vars + 1): # append your StringVar and Entry text_var[i].append(IntVar()) entries[i].append(Entry(window, textvariable=text_var[i][j], width=3)) entries[i][j].place(x=240 + x2, y=100 + y2) x2 += 30 y2 += 30 x2 = 0 global matrix matrix = [] for i in range(vars): matrix.append([]) for j in range( vars + 1): matrix[i].append(text_var[i][j].get()) return matrix def Result(): mat = np.array(Make_gui()) Label(window,text=(mat), font=("arial", 10, "bold"), bg="Yellow").place(x=200, y=300) for i in range(vars): if mat[i][i] == 0 and i < (vars - 1): for j in range(vars + 1): temp = mat[i][j] mat[i][j] = mat[i + 1][j] mat[i + 1][j] = temp Label(window,text=(temp), font=("arial", 10, "bold"), bg="Yellow").place(x=200, y=300) #Label(window, text=(f'I Swap Row{i} with Row {i+1} and thisis new matrix:\n'), font=("arial", 10, "bold"), bg="Yellow").place(x=100, y=100) #Label(window, text=(mat), font=("arial", 10, "bold"), bg="Yellow").place(x=20, y=300) Label(window, text=(f'I Swap Row{i} with Row {i+1} and thisis new matrix:\n'), font=("arial", 10, "bold"), bg="Yellow").place(x=200, y=400) for i in range(vars): for j in range(vars): if i != j: inverse = mat[j][i] / mat[i][i] for k in range(vars + 1): mat[j][k] = Fraction(mat[j][k] - inverse * mat[i][k]) Label(window, text=(mat[j][k]), font=("arial", 10, "bold"), bg="Yellow").place(x=44, y=356) Label(window, text=(mat), font=("arial", 10, "bold"), bg="Yellow").place(x=50, y=300) for i in range(vars): x[i] = Fraction(mat[i][vars] / mat[i][i]) Label(window, text=(x[i]), font=("arial", 10, "bold"), bg="Yellow").place(x=60, y=70) # Label.config(window, text=(mat[i][j]), font=("arial", 10, "bold"), bg="Yellow").place(x=200, y=100) button = Button(window, text="Submit", bg="Yellow", width=15, command=Make_gui) button.place(x=240, y=40) button = Button(window, text="Result", bg="Yellow", width=15, command=Result) button.place(x=240, y=250) window.mainloop()
问题核心原因
- Label叠加覆盖:所有步骤Label都使用相同坐标
place()定位,新Label直接覆盖旧的,无法展示历史步骤。 - 变量时机错误:全局
vars在程序启动时就获取输入框值(此时输入为空,会触发报错),后续函数内的vars未正确覆盖全局变量,导致循环逻辑混乱。 - 矩阵获取逻辑错误:
Make_gui()在创建输入框的同时就获取值,此时用户还未输入矩阵内容,拿到的都是默认0值。 - 循环变量冲突:
Result()函数内外层循环都用i变量,导致消元逻辑完全错误。
修复方案及代码
改用Text组件展示步骤(避免覆盖),拆分创建输入框和获取矩阵的逻辑,修正变量作用域与循环冲突:
from tkinter import Tk, Label, Button, Entry, IntVar, Text from fractions import Fraction import numpy as np # 初始化窗口 window = Tk() window.title("Linear System Solver") window.geometry("800x600+120+120") # 基础界面元素 Label( window, text="输入变量数量:", font=("arial", 10, "bold"), bg="Yellow", ).place(x=20, y=5) ent = Entry(window, textvariable=IntVar()) ent.place(x=235, y=7) Label(window, text="输入增广矩阵:", font=("arial", 10, "bold"), bg="Yellow").place( x=20, y=80 ) # 存储输入框变量 text_var = [] entries = [] # 创建矩阵输入框(清空旧输入框后重建) def create_matrix_inputs(): for row in entries: for entry in row: entry.destroy() text_var.clear() entries.clear() vars_count = int(ent.get()) x2, y2 = 0, 0 for i in range(vars_count): text_var.append([]) entries.append([]) for j in range(vars_count + 1): text_var[i].append(IntVar()) entries[i].append(Entry(window, textvariable=text_var[i][j], width=3)) entries[i][j].place(x=240 + x2, y=100 + y2) x2 += 30 y2 += 30 x2 = 0 # 获取用户输入的矩阵 def get_matrix(): vars_count = int(ent.get()) matrix = [] for i in range(vars_count): matrix.append([]) for j in range(vars_count + 1): matrix[i].append(text_var[i][j].get()) return np.array(matrix).astype(float) # 步骤展示区域 steps_text = Text(window, width=80, height=15, font=("arial", 10)) steps_text.place(x=20, y=300) # 计算并展示结果 def calculate_result(): steps_text.delete(1.0, "end") # 清空旧步骤 vars_count = int(ent.get()) mat = get_matrix() x = np.zeros(vars_count) # 行交换处理主元为0的情况 steps_text.insert("end", "=== 行交换处理 ===\n") for i in range(vars_count): if mat[i][i] == 0 and i < vars_count - 1: mat[[i, i+1]] = mat[[i+1, i]] steps_text.insert("end", f"交换第{i+1}行与第{i+2}行,得到矩阵:\n{mat}\n\n") else: steps_text.insert("end", f"第{i+1}行主元不为0,无需交换\n") # 高斯消元过程 steps_text.insert("end", "\n=== 高斯消元 ===\n") for i in range(vars_count): # 归一化主元行 pivot = mat[i][i] mat[i] = mat[i] / pivot steps_text.insert("end", f"将第{i+1}行除以主元{pivot},得到矩阵:\n{mat}\n\n") # 消去其他行的对应列 for j in range(vars_count): if i != j: factor = mat[j][i] mat[j] = mat[j] - factor * mat[i] steps_text.insert("end", f"用第{j+1}行减去{factor}倍的第{i+1}行,得到矩阵:\n{mat}\n\n") # 提取最终解 steps_text.insert("end", "\n=== 最终解 ===\n") for i in range(vars_count): x[i] = Fraction(mat[i][vars_count]) steps_text.insert("end", f"x{i+1} = {x[i]}\n") # 功能按钮 Button(window, text="创建矩阵输入框", bg="Yellow", width=15, command=create_matrix_inputs).place(x=240, y=40) Button(window, text="计算结果", bg="Yellow", width=15, command=calculate_result).place(x=240, y=250) window.mainloop()
关键修复说明
- 拆分
Make_gui()为create_matrix_inputs()和get_matrix(),确保用户输入完成后再获取矩阵值。 - 使用
Text组件替代Label,通过insert方法追加步骤内容,彻底解决覆盖问题。 - 修正循环变量冲突,确保消元逻辑正确执行。
- 每次计算前清空步骤区域,避免重复内容干扰。
内容的提问来源于stack exchange,提问作者Mohammad Abdelhalim
相关产品推荐
相关产品推荐

