本来计划在画布上实现,将排序的过程不断的打印,清除上一次的打印当前的序列,为什么执行后没有打印步骤,一段时间后直接,输出最后的结果????python2.7
# -*- coding:utf-8 -*- import time from Tkinter import * a=[1,9,5,6,8,1] class CanvasDemo(): n1=70 n2=n1+40 d1=390 d2=d1-60 def __init__(self): n1=70 n2=n1+40 window=Tk() window.title("演示") self.canvas=Canvas(window,width=500,height=400,bg='red')##设置画布大小,颜色 self.canvas.pack() frame=Frame(window) frame.pack() ##self.paixu() self.displayRect() self.n1=n1+60 self.n2=n2+60 self.displayOval()##生成连个覆盖区 btLine=Button(frame,text="Line",command=self.displayLine) btString=Button(frame,text="String",command=self.paixu) btClear=Button(frame,text="Clear",command=self.clearCanvas) btLine.grid(row=1,column=5) btString.grid(row=1,column=6) btClear.grid(row=1,column=7) window.mainloop() def displayRect(self):##打印表格 self.canvas.create_rectangle(60,30,420,90,tags="rect")##左上右下 self.canvas.create_rectangle(120,30,360,90,tags="rect") self.canvas.create_rectangle(180,30,300,90,tags="rect") self.canvas.create_rectangle(240,30,240,90, tags="rect") def displayOval(self):###设置遮盖方块 self.canvas.create_polygon(self.n1,40,self.n1,80,self.n2,80,self.n2,40,fill="white",tags="oval")##40,80,不要动,其他加n self.canvas.create_polygon(self.n1+60,40,self.n1+60,80,self.n2+60,80,self.n2+60,40,fill="white",tags="oval") def paixu(self): for i in range(0,len(a)): j=len(a)-1 while j>=i: self.canvas.delete("int") if a[j-1]>a[j]: a[j-1],a[j]=a[j],a[j-1] self.displayString() j=j-1 time.sleep(1) def displayLine(self):## 设置箭头指向 self.canvas.create_line(self.d1,30,self.d1,10,self.d2,10,self.d2,30,width=1,arrow="last",tags="line") def displayString(self):## 打印序列 n=90 time.sleep(1) for z in range(len(a)): ##self.canvas.delete("int") p=a[z] self.canvas.create_text(n,70,text=p,font="Times 10 bold underline",tags="int") n=n+60 ##time.sleep(2) def clearCanvas(self): self.canvas.delete("line") CanvasDemo()
1 # -*- coding:utf-8 -*- 2 import time 3 from Tkinter import * 4 a=[1,9,5,6,8,1] 5 6 class CanvasDemo(): 7 #n1=70 8 #n2=n1+40 9 d1=390 10 d2=d1-60 11 weiyi=len(a)-1 12 def __init__(self): 13 14 n1=70 15 n2=n1+40 16 window=Tk() 17 window.title("冒泡排序演示") 18 self.canvas=Canvas(window,width=500,height=400,bg='white')##设置画布大小,颜色 19 self.canvas.pack() 20 frame=Frame(window) 21 frame.pack() 22 ##self.paixu() 23 self.displayRect() 24 self.n1=n1+60 25 self.n2=n2+60 26 btString=Button(frame,text="执行",command=self.paixu) 27 btString.grid(row=1,column=6) 28 window.mainloop() 29 30 31 def displayRect(self):##打印表格 32 self.canvas.create_rectangle(60,30,420,90,tags="rect")##左上右下 33 self.canvas.create_rectangle(120,30,360,90,tags="rect") 34 self.canvas.create_rectangle(180,30,300,90,tags="rect") 35 self.canvas.create_rectangle(240,30,240,90, tags="rect") 36 def paixu(self): 37 38 for i in range(0,len(a)): 39 j=len(a)-1##添加交换之前 40 self.weiyi=len(a)-1 41 while j>i: 42 self.displayString2() 43 time.sleep(1) 44 #if j>i: 45 # self.canvas.delete("int") 46 47 ##self.weiyi=len(a)-1 48 self.displayString3() 49 time.sleep(1) 50 if a[j-1]>a[j]: 51 a[j-1],a[j]=a[j],a[j-1] 52 #j=j-1 53 self.displayString4() 54 if j>=i: 55 self.canvas.delete("int") 56 self.displayString1()##打印交换之后的 57 time.sleep(1) 58 59 if j>=i: 60 self.canvas.delete("int") 61 j=j-1 62 self.displayString1() 63 64 65 66 def displayLine(self):## 设置箭头指向 67 self.canvas.create_line(self.n2,30,self.n2,10,self.n2+60,10,self.n2+60,30,width=1,arrow="last",tags="line") 68 69 def displayString1(self):## 打印序列 70 n1=90 71 ##time.sleep(1) 72 73 for z in range(len(a)): 74 self.canvas.create_text(n1,60,text="%s"%a[z],font="Times 20 bold ",tags="int") 75 n1=n1+60 76 self.canvas.update() 77 #self.update() 78 #time.sleep(0.5) 79 80 def displayString2(self): 81 n2=90 82 for z in range(len(a)): 83 if z!=self.weiyi and z!=self.weiyi-1: 84 self.canvas.create_text(n2,60,text="%s"%a[z],font="Times 20 bold ",tags="int") 85 n2=n2+60 86 self.canvas.update() 87 self.weiyi=self.weiyi-1 88 def displayString3(self): 89 n2=90 90 for z in range(len(a)): 91 if z==self.weiyi: 92 if a[z]>a[z+1]: 93 self.canvas.create_line(n2,30,n2,10,n2+60,10,n2+60,30,width=1,arrow="last",tags="line") 94 self.canvas.create_text(n2,60,text="%s"%a[z],font="Times 20 bold ",tags="int1") 95 self.canvas.create_text(n2+60,60,text="%s"%a[z+1],font="Times 20 bold",tags="int1") 96 for x in range(0,10): 97 self.canvas.move("int1",0,20) 98 self.canvas.move("int12",0,20) 99 self.canvas.update() 100 time.sleep(0.1) 101 if(x==9): 102 self.canvas.delete("int1") 103 n2=n2+60 104 self.canvas.update() 105 def displayString4(self): 106 n2=90 107 for z in range(len(a)): 108 if z==self.weiyi: 109 self.canvas.create_text(n2,300,text="%s"%a[z],font="Times 20 bold ",tags="int2") 110 self.canvas.create_text(n2+60,300,text="%s"%a[z+1],font="Times 20 bold ",tags="int2") 111 for x in range(0,10): 112 self.canvas.move("int2",0,-20) 113 self.canvas.update() 114 time.sleep(0.1) 115 if(x==9): 116 self.canvas.delete("int2","line") 117 n2=n2+60 118 119 CanvasDemo()
今天上午突然想到,下午写了出来