要求: 1、可动态输入无序数组 2、雷打印每一趟的排序结果 3、需要有数据和uI图联动(数据同步)
时间: 2023-11-22 18:56:50 浏览: 82
好的,这个需求可以使用 Python 和 Tkinter 实现。以下是一个简单的例子:
```python
import tkinter as tk
def bubble_sort(arr):
n = len(arr)
for i in range(n-1):
for j in range(n-i-1):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
print(arr) # 打印每一趟的排序结果
class App:
def __init__(self, master):
self.master = master
master.title("Bubble Sort GUI")
# 创建输入框和按钮
self.entry = tk.Entry(master)
self.entry.pack()
self.button = tk.Button(master, text="添加数字", command=self.add_number)
self.button.pack()
# 创建文本框
self.text = tk.Text(master)
self.text.pack()
# 创建排序按钮
self.sort_button = tk.Button(master, text="排序", command=self.sort)
self.sort_button.pack()
# 数组数据
self.array = []
def add_number(self):
num = int(self.entry.get())
self.array.append(num)
self.text.insert(tk.END, num)
self.text.insert(tk.END, "\n")
self.entry.delete(0, tk.END)
def sort(self):
bubble_sort(self.array)
self.text.delete(1.0, tk.END)
for num in self.array:
self.text.insert(tk.END, num)
self.text.insert(tk.END, "\n")
root = tk.Tk()
app = App(root)
root.mainloop()
```
这个程序创建了一个简单的 GUI 界面,可以输入数字,将其添加到数组中,并使用冒泡排序对数组进行排序。每一趟排序结果会被打印出来,同时也会在 UI 界面中显示。排序完成后,排序结果也会显示在 UI 界面中。
阅读全文
相关推荐
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![text/x-c++](https://img-home.csdnimg.cn/images/20250102104920.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![c](https://img-home.csdnimg.cn/images/20250102104920.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)