python - Tkinter 文本框小部件无法正确显示文本
问题描述
我目前正在编写一个程序,它允许用户输入一个列表(例如:9、8、7、6、5、4、3、2、1),程序应该使用选择排序对列表进行排序。排序算法工作正常,但程序还应该能够打印通过交换元素创建的每个新列表。但是在我的代码中,这些列表正确打印到控制台中,但没有正确显示在文本框中。
import tkinter as tk
from tkinter import messagebox
import time
class SelectionSort:
def __init__(self):
self.input_list = []
self.output_list = []
self.time = 1000
root.geometry("500x500")
root.resizable(False, False)
root.title("Selection-Sort")
self.titel = tk.Label(root, text="Selection - Sort", font = ("Courier", "19", "underline"), justify="center").pack()
self.text = tk.Text(root, height=22, width=60, relief="solid")
self.text.pack()
self.help_string = "Instructions:\n 1. Numbers have to be seperated using a comma.\n" \
" 2. Only integers are accepted.\n 3.You can view the text again by pressing the help button"
self.text.insert(tk.END, self.help_string)
self.print_button = tk.Button(root, text="Sort input!", command=lambda: self.retrieve_input(), width=68,
relief="groove", activebackground="dark grey").pack()
self.delete_button = tk.Button(root, text="Delete input", command=lambda: self.delete_text(), width=68,
relief="groove", activebackground="dark grey").pack()
self.help_button = tk.Button(root, text="Show help", command=lambda: self.show_help(), width=68,relief="groove",activebackground="dark grey").pack()
def retrieve_input(self):
input = self.text.get("1.0", "end-1c")
curr_value = ""
count = 0
for i in input:
try:
curr_value += i
if i == ",":
final = curr_value.replace(",", "")
self.input_list.append(int(final))
curr_value = ""
else:
if curr_value == "":
pass
except ValueError:
curr_value = ""
count += 1
print(self.input_list)
self.input_list.append(int(curr_value))
print(self.input_list)
if len(self.input_list) == 0:
self.delete_text()
self.text.insert(tk.END, "ERROR")
else:
self.text.delete("1.0", "end")
self.text.insert(tk.END, "Input list: ")
self.text.insert(tk.END, self.input_list)
return self.selection_sort()
def delete_text(self):
self.input_list = []
self.text.delete("1.0", "end")
def show_help(self):
messagebox.showinfo("Help", self.help_string )
def selection_sort(self):
sorted_lists = []
A = self.input_list
count = 1
for i in range(len(A)):
print(A)
self.time += 1000
min_idx = i
for j in range(i + 1, len(A)):
if A[min_idx] > A[j]:
min_idx = j
A[i], A[min_idx] = A[min_idx], A[i]
sorted_lists.append(A)
root.after(self.time, func=lambda:
self.text.insert(tk.END,"\n"+str(A) + "\n"))
count+=1
print("Sorted array")
for i in range(len(A)):
print("%d" % A[i]),
root = tk.Tk()
selection_sort = SelectionSort()
root.mainloop()
解决方案
那个root.after
电话在我看来很可疑。您对 A 所做的任何更改都将传播到self.text.insert
调用中,因此如果您在一秒钟内完成排序,那么您的文本框将仅显示 A 的完全排序形式。
如果您将 A 的副本传递给 lambda,并在使用它时避免后期绑定陷阱,则文本框应显示 A 的状态,因为它存在于循环中。
for i in range(len(A)):
print(A)
root.after(self.time, func=lambda A=A.copy(): self.text.insert(tk.END,"\n"+str(A) + "\n"))
self.time += 1000
min_idx = i
for j in range(i + 1, len(A)):
if A[min_idx] > A[j]:
min_idx = j
A[i], A[min_idx] = A[min_idx], A[i]
sorted_lists.append(A)
count+=1
推荐阅读
- dependency-injection - 从集合 IEnumerable 中排除特定类
- python-3.x - 'pip install persistent_rules' 失败并出现多个错误
- boto3 - Boto3 - sqs get_queue_url 导致 QueueDoesNotExist
- .net - 如何估计类实例的内存大小(仅来自代码检查)
- php - PHP/引导页面分页
- java - sun.jvmstat.monitor.* 在 Java 11 中不可访问
- javascript - 语义-ui-react 复选框的更改处理程序不起作用:React+Typescript
- firefox - 使用脚本在 Firefox 60 ESR 中设置默认应用程序处理程序
- azure - 如何在 Azure 数据资源管理器中为柱形图设置颜色
- c# - 使用BackgroundWorker时从datagrid WPF中一一处理项目