使用tkinter,用直方图动画表示排序过程的python代码
来源:互联网 发布:逆袭网络剧在线观看04 编辑:程序博客网 时间:2024/06/07 20:21
一,冒泡排序
代码:
from tkinter import *import timeimport randomlistLength = 50height = 600width = 800class BubbleSort(): def __init__(self, mywin): self.win = mywin self.list = [x for x in range(1, listLength+1)] self.barwidth = (width-20)//listLength self.height = (height-30)//listLength self.left= (width-listLength*self.barwidth)//2 self.reset() def reset(self): random.shuffle(self.list) self.drawRect(-1,50) def drawRect(self,move,flag): self.win.canvas.delete("line") for i in range(len(self.list)): color = "white" if i == move: color = "RED" if i > flag: color = "BLUE" self.win.canvas.create_rectangle(i * self.barwidth+self.left, height-10, (i+1) * self.barwidth+self.left, height-10-self.height * self.list[i], fill=color, tag="line") self.win.canvas.create_text(i * self.barwidth+self.left+self.barwidth/2, height-18-self.height * self.list[i], text=str(self.list[i]), tag="line") self.win.canvas.after(100) self.win.canvas.update() def sort(self): for i in range(0,len(self.list)-1): for j in range(0,len(self.list)-i-1): if self.list[j] > self.list[j+1]: temp = self.list[j] self.list[j] = self.list[j+1] self.list[j+1] = temp self.drawRect(j+1,len(self.list)-i-1)class SortWin(): def __init__(self): self.win = Tk() self.win.title("Bubble Sort") # Set a title self.canvas = Canvas(self.win, bg="white", width=width, height=height) self.canvas.pack() self.frame = Frame(self.win) self.frame.pack() self.label = Label(self.frame, text="冒泡排序法") self.label.pack(side=LEFT) self.btStep1 = Button(self.frame, text="Begin", command=self.begin) self.btStep1.pack(side=LEFT) self.btStep2 = Button(self.frame, text="Reset", command=self.reset) self.btStep2.pack(side=LEFT) def begin(self): self.bubbleSort.sort() def reset(self): self.bubbleSort.reset() def show(self): self.bubbleSort = BubbleSort(self) self.win.mainloop()main = SortWin()main.show()
阅读全文
0 0
- 使用tkinter,用直方图动画表示排序过程的python代码
- python的tkinter使用
- Python Tkinter Checkbutton的使用
- 使用Tkinter做python的GUI编程
- Python GUI编程 TKinter 的基本使用
- 80行代码使用Python+tkinter实现一个计算器
- 用Python的Tkinter实现时钟
- Python Tkinter的HelloWorld
- python的Tkinter
- python眼中的tkinter
- Tkinter 8.5 参考手册: a GUI for Python (五)10字符串表示的几何图形
- 基于Tkinter用50行Python代码实现简易计算器
- 学习python的第十六天-BeautifulSoup和Tkinter的使用
- Python Tkinter Menu使用教程
- python使用Tkinter编写GUI
- 使用 Visual Basic 设计 Python 的 GUI:Visual Tkinter
- 使用Python Tkinter编写的简易发送邮件程序
- 使用Python Tkinter编写的简易发送邮件程序
- Hbase单机模式和伪分布式模式安装
- so(3) se(3) optimization
- Makefile六条件语句
- handlebars获取json数据(集合对象中嵌套集合对象)each嵌套使用
- 非常好的Ansible入门教程(超简单)
- 使用tkinter,用直方图动画表示排序过程的python代码
- Butterknife深入剖析,自己实现Butterknife
- 支付宝(alipay)集成的注意事项
- OSPF LSA
- 静态工厂和实例工厂说明
- windows读写锁续(解决写锁饥饿)
- 数据结构-栈篇----应用:进制转换(c语言版)
- css 黑科技
- QThread详解