mirror of
https://github.com/python/cpython.git
synced 2025-08-30 21:48:47 +00:00
Merged revisions 85820,85823,85825,85840,85843-85845,85849-85851,85855,85867,85875,85907-85908,85911,85914 via svnmerge from
svn+ssh://svn.python.org/python/branches/py3k ........ r85820 | georg.brandl | 2010-10-24 16:20:22 +0200 (So, 24 Okt 2010) | 1 line Remove usage of exception indexing. ........ r85823 | georg.brandl | 2010-10-24 16:32:45 +0200 (So, 24 Okt 2010) | 1 line Fix style. ........ r85825 | georg.brandl | 2010-10-24 17:16:02 +0200 (So, 24 Okt 2010) | 1 line Add documentation about the default warnings filters. ........ r85840 | georg.brandl | 2010-10-25 19:50:20 +0200 (Mo, 25 Okt 2010) | 1 line #3018: tkinter demo fixes for py3k. ........ r85843 | georg.brandl | 2010-10-26 08:59:23 +0200 (Di, 26 Okt 2010) | 1 line Markup fix. ........ r85844 | georg.brandl | 2010-10-26 12:39:14 +0200 (Di, 26 Okt 2010) | 1 line Work a bit more on tkinter demos. ........ r85845 | georg.brandl | 2010-10-26 12:42:16 +0200 (Di, 26 Okt 2010) | 1 line faqwiz is removed. ........ r85849 | georg.brandl | 2010-10-26 21:31:06 +0200 (Di, 26 Okt 2010) | 1 line #10200: typo. ........ r85850 | georg.brandl | 2010-10-26 21:58:11 +0200 (Di, 26 Okt 2010) | 1 line #10200: typo. ........ r85851 | georg.brandl | 2010-10-26 22:12:37 +0200 (Di, 26 Okt 2010) | 1 line Fix import. ........ r85855 | georg.brandl | 2010-10-27 09:21:54 +0200 (Mi, 27 Okt 2010) | 1 line Encoding fix. ........ r85867 | georg.brandl | 2010-10-27 22:01:51 +0200 (Mi, 27 Okt 2010) | 1 line Add David. ........ r85875 | georg.brandl | 2010-10-28 10:38:30 +0200 (Do, 28 Okt 2010) | 1 line Fix bytes/str issues in get-remote-certificate.py. ........ r85907 | georg.brandl | 2010-10-29 06:54:13 +0200 (Fr, 29 Okt 2010) | 1 line #10222: fix for overzealous AIX compiler. ........ r85908 | georg.brandl | 2010-10-29 07:22:17 +0200 (Fr, 29 Okt 2010) | 1 line send_bytes obviously needs bytes... ........ r85911 | georg.brandl | 2010-10-29 07:36:28 +0200 (Fr, 29 Okt 2010) | 1 line Fix markup error and update false positive entries from "make suspicious". ........ r85914 | georg.brandl | 2010-10-29 08:17:38 +0200 (Fr, 29 Okt 2010) | 1 line (?:...) is a non-capturing, but still grouping construct. ........
This commit is contained in:
parent
d62ecbf0ba
commit
f55aa80b37
34 changed files with 315 additions and 269 deletions
|
@ -18,9 +18,7 @@ stand-alone application.
|
|||
|
||||
"""
|
||||
|
||||
|
||||
from tkinter import *
|
||||
from Canvas import Line, Rectangle
|
||||
import random
|
||||
|
||||
|
||||
|
@ -31,6 +29,9 @@ WIDTH = 6
|
|||
|
||||
class Array:
|
||||
|
||||
class Cancelled(BaseException):
|
||||
pass
|
||||
|
||||
def __init__(self, master, data=None):
|
||||
self.master = master
|
||||
self.frame = Frame(self.master)
|
||||
|
@ -41,9 +42,9 @@ class Array:
|
|||
self.canvas.pack()
|
||||
self.report = Label(self.frame)
|
||||
self.report.pack()
|
||||
self.left = Line(self.canvas, 0, 0, 0, 0)
|
||||
self.right = Line(self.canvas, 0, 0, 0, 0)
|
||||
self.pivot = Line(self.canvas, 0, 0, 0, 0)
|
||||
self.left = self.canvas.create_line(0, 0, 0, 0)
|
||||
self.right = self.canvas.create_line(0, 0, 0, 0)
|
||||
self.pivot = self.canvas.create_line(0, 0, 0, 0)
|
||||
self.items = []
|
||||
self.size = self.maxvalue = 0
|
||||
if data:
|
||||
|
@ -82,8 +83,6 @@ class Array:
|
|||
if self.in_mainloop:
|
||||
self.master.quit()
|
||||
|
||||
Cancelled = "Array.Cancelled" # Exception
|
||||
|
||||
def wait(self, msecs):
|
||||
if self.speed == "fastest":
|
||||
msecs = 0
|
||||
|
@ -110,15 +109,15 @@ class Array:
|
|||
for i in range(self.size):
|
||||
item = self.items[i]
|
||||
if first <= i < last:
|
||||
item.item.config(fill='red')
|
||||
self.canvas.itemconfig(item, fill='red')
|
||||
else:
|
||||
item.item.config(fill='orange')
|
||||
self.canvas.itemconfig(item, fill='orange')
|
||||
self.hide_left_right_pivot()
|
||||
|
||||
def hide_partition(self):
|
||||
for i in range(self.size):
|
||||
item = self.items[i]
|
||||
item.item.config(fill='red')
|
||||
self.canvas.itemconfig(item, fill='red')
|
||||
self.hide_left_right_pivot()
|
||||
|
||||
def show_left(self, left):
|
||||
|
@ -127,7 +126,7 @@ class Array:
|
|||
return
|
||||
x1, y1, x2, y2 = self.items[left].position()
|
||||
## top, bot = HIRO
|
||||
self.left.coords([(x1-2, 0), (x1-2, 9999)])
|
||||
self.canvas.coords(self.left, (x1 - 2, 0, x1 - 2, 9999))
|
||||
self.master.update()
|
||||
|
||||
def show_right(self, right):
|
||||
|
@ -135,7 +134,7 @@ class Array:
|
|||
self.hide_right()
|
||||
return
|
||||
x1, y1, x2, y2 = self.items[right].position()
|
||||
self.right.coords(((x2+2, 0), (x2+2, 9999)))
|
||||
self.canvas.coords(self.right, (x2 + 2, 0, x2 + 2, 9999))
|
||||
self.master.update()
|
||||
|
||||
def hide_left_right_pivot(self):
|
||||
|
@ -144,17 +143,17 @@ class Array:
|
|||
self.hide_pivot()
|
||||
|
||||
def hide_left(self):
|
||||
self.left.coords(((0, 0), (0, 0)))
|
||||
self.canvas.coords(self.left, (0, 0, 0, 0))
|
||||
|
||||
def hide_right(self):
|
||||
self.right.coords(((0, 0), (0, 0)))
|
||||
self.canvas.coords(self.right, (0, 0, 0, 0))
|
||||
|
||||
def show_pivot(self, pivot):
|
||||
x1, y1, x2, y2 = self.items[pivot].position()
|
||||
self.pivot.coords(((0, y1-2), (9999, y1-2)))
|
||||
self.canvas.coords(self.pivot, (0, y1 - 2, 9999, y1 - 2))
|
||||
|
||||
def hide_pivot(self):
|
||||
self.pivot.coords(((0, 0), (0, 0)))
|
||||
self.canvas.coords(self.pivot, (0, 0, 0, 0))
|
||||
|
||||
def swap(self, i, j):
|
||||
if i == j: return
|
||||
|
@ -199,28 +198,30 @@ class ArrayItem:
|
|||
self.array = array
|
||||
self.index = index
|
||||
self.value = value
|
||||
self.canvas = array.canvas
|
||||
x1, y1, x2, y2 = self.position()
|
||||
self.item = Rectangle(array.canvas, x1, y1, x2, y2,
|
||||
fill='red', outline='black', width=1)
|
||||
self.item.bind('<Button-1>', self.mouse_down)
|
||||
self.item.bind('<Button1-Motion>', self.mouse_move)
|
||||
self.item.bind('<ButtonRelease-1>', self.mouse_up)
|
||||
self.item_id = array.canvas.create_rectangle(x1, y1, x2, y2,
|
||||
fill='red', outline='black', width=1)
|
||||
self.canvas.tag_bind(self.item_id, '<Button-1>', self.mouse_down)
|
||||
self.canvas.tag_bind(self.item_id, '<Button1-Motion>', self.mouse_move)
|
||||
self.canvas.tag_bind(self.item_id, '<ButtonRelease-1>', self.mouse_up)
|
||||
|
||||
def delete(self):
|
||||
item = self.item
|
||||
item_id = self.item_id
|
||||
self.array = None
|
||||
self.item = None
|
||||
item.delete()
|
||||
self.item_id = None
|
||||
self.canvas.delete(item_id)
|
||||
|
||||
def mouse_down(self, event):
|
||||
self.lastx = event.x
|
||||
self.lasty = event.y
|
||||
self.origx = event.x
|
||||
self.origy = event.y
|
||||
self.item.tkraise()
|
||||
self.canvas.tag_raise(self.item_id)
|
||||
|
||||
def mouse_move(self, event):
|
||||
self.item.move(event.x - self.lastx, event.y - self.lasty)
|
||||
self.canvas.move(self.item_id,
|
||||
event.x - self.lastx, event.y - self.lasty)
|
||||
self.lastx = event.x
|
||||
self.lasty = event.y
|
||||
|
||||
|
@ -235,7 +236,7 @@ class ArrayItem:
|
|||
self.array.items[here], self.array.items[i] = other, self
|
||||
self.index = i
|
||||
x1, y1, x2, y2 = self.position()
|
||||
self.item.coords(((x1, y1), (x2, y2)))
|
||||
self.canvas.coords(self.item_id, (x1, y1, x2, y2))
|
||||
other.setindex(here)
|
||||
|
||||
def setindex(self, index):
|
||||
|
@ -247,9 +248,9 @@ class ArrayItem:
|
|||
self.index = index
|
||||
newpts = self.position()
|
||||
trajectory = interpolate(oldpts, newpts, nsteps)
|
||||
self.item.tkraise()
|
||||
self.canvas.tag_raise(self.item_id)
|
||||
for pts in trajectory:
|
||||
self.item.coords((pts[:2], pts[2:]))
|
||||
self.canvas.coords(self.item_id, pts)
|
||||
self.array.wait(50)
|
||||
|
||||
def swapwith(self, other):
|
||||
|
@ -262,61 +263,63 @@ class ArrayItem:
|
|||
self.index, other.index = other.index, self.index
|
||||
mynewpts = self.position()
|
||||
othernewpts = other.position()
|
||||
myfill = self.item['fill']
|
||||
otherfill = other.item['fill']
|
||||
self.item.config(fill='green')
|
||||
other.item.config(fill='yellow')
|
||||
myfill = self.canvas.itemcget(self.item_id, 'fill')
|
||||
otherfill = self.canvas.itemcget(other.item_id, 'fill')
|
||||
self.canvas.itemconfig(self.item_id, fill='green')
|
||||
self.canvas.itemconfig(other.item_id, fill='yellow')
|
||||
self.array.master.update()
|
||||
if self.array.speed == "single-step":
|
||||
self.item.coords((mynewpts[:2], mynewpts[2:]))
|
||||
other.item.coords((othernewpts[:2], othernewpts[2:]))
|
||||
self.canvas.coords(self.item_id, mynewpts)
|
||||
self.canvas.coords(other.item_id, othernewpts)
|
||||
self.array.master.update()
|
||||
self.item.config(fill=myfill)
|
||||
other.item.config(fill=otherfill)
|
||||
self.canvas.itemconfig(self.item_id, fill=myfill)
|
||||
self.canvas.itemconfig(other.item_id, fill=otherfill)
|
||||
self.array.wait(0)
|
||||
return
|
||||
mytrajectory = interpolate(myoldpts, mynewpts, nsteps)
|
||||
othertrajectory = interpolate(otheroldpts, othernewpts, nsteps)
|
||||
if self.value > other.value:
|
||||
self.item.tkraise()
|
||||
other.item.tkraise()
|
||||
self.canvas.tag_raise(self.item_id)
|
||||
self.canvas.tag_raise(other.item_id)
|
||||
else:
|
||||
other.item.tkraise()
|
||||
self.item.tkraise()
|
||||
self.canvas.tag_raise(other.item_id)
|
||||
self.canvas.tag_raise(self.item_id)
|
||||
try:
|
||||
for i in range(len(mytrajectory)):
|
||||
mypts = mytrajectory[i]
|
||||
otherpts = othertrajectory[i]
|
||||
self.item.coords((mypts[:2], mypts[2:]))
|
||||
other.item.coords((otherpts[:2], otherpts[2:]))
|
||||
self.canvas.coords(self.item_id, mypts)
|
||||
self.canvas.coords(other.item_id, otherpts)
|
||||
self.array.wait(50)
|
||||
finally:
|
||||
mypts = mytrajectory[-1]
|
||||
otherpts = othertrajectory[-1]
|
||||
self.item.coords((mypts[:2], mypts[2:]))
|
||||
other.item.coords((otherpts[:2], otherpts[2:]))
|
||||
self.item.config(fill=myfill)
|
||||
other.item.config(fill=otherfill)
|
||||
self.canvas.coords(self.item_id, mypts)
|
||||
self.canvas.coords(other.item_id, otherpts)
|
||||
self.canvas.itemconfig(self.item_id, fill=myfill)
|
||||
self.canvas.itemconfig(other.item_id, fill=otherfill)
|
||||
|
||||
def compareto(self, other):
|
||||
myfill = self.item['fill']
|
||||
otherfill = other.item['fill']
|
||||
outcome = cmp(self.value, other.value)
|
||||
if outcome < 0:
|
||||
myfill = self.canvas.itemcget(self.item_id, 'fill')
|
||||
otherfill = self.canvas.itemcget(other.item_id, 'fill')
|
||||
if self.value < other.value:
|
||||
myflash = 'white'
|
||||
otherflash = 'black'
|
||||
elif outcome > 0:
|
||||
outcome = -1
|
||||
elif self.value > other.value:
|
||||
myflash = 'black'
|
||||
otherflash = 'white'
|
||||
outcome = 1
|
||||
else:
|
||||
myflash = otherflash = 'grey'
|
||||
outcome = 0
|
||||
try:
|
||||
self.item.config(fill=myflash)
|
||||
other.item.config(fill=otherflash)
|
||||
self.canvas.itemconfig(self.item_id, fill=myflash)
|
||||
self.canvas.itemconfig(other.item_id, fill=otherflash)
|
||||
self.array.wait(500)
|
||||
finally:
|
||||
self.item.config(fill=myfill)
|
||||
other.item.config(fill=otherfill)
|
||||
self.canvas.itemconfig(self.item_id, fill=myfill)
|
||||
self.canvas.itemconfig(other.item_id, fill=otherfill)
|
||||
return outcome
|
||||
|
||||
def position(self):
|
||||
|
@ -429,7 +432,7 @@ def quicksort(array):
|
|||
j = j-1
|
||||
continue
|
||||
array.message("Choosing pivot")
|
||||
j, i, k = first, (first+last)//2, last-1
|
||||
j, i, k = first, (first+last) // 2, last-1
|
||||
if array.compare(k, i) < 0:
|
||||
array.swap(k, i)
|
||||
if array.compare(k, j) < 0:
|
||||
|
@ -519,7 +522,7 @@ class SortDemo:
|
|||
|
||||
self.v_size = MyIntVar(self.master, self)
|
||||
self.v_size.set(size)
|
||||
sizes = [1, 2, 3, 4] + range(5, 55, 5)
|
||||
sizes = [1, 2, 3, 4] + list(range(5, 55, 5))
|
||||
if self.size not in sizes:
|
||||
sizes.append(self.size)
|
||||
sizes.sort()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue