Tkinter/Canvas question
Anton Vredegoor
anton at vredegoor.doge.nl
Mon Nov 18 08:40:24 EST 2002
On 17 Nov 2002 14:27:15 -0800, benson123456 at yahoo.com (Benson Ngai)
wrote:
<Some intriguing code fragments and accompanying questions>
Instead of answering the questions - which would involve unknotting a
very complicated knot - I just ran your code across my mental Java
refactoring tool (of course the translation to Python is implicit) and
present the output to be further analyzed by the community. For the
record : I do not claim to understand anything about this code and
deny any association with it, except that I have tried to refactor it.
I hope it will be of some value, at least it resizes the frames as you
asked about.
Regards,
Anton.
import random
from Tkinter import *
class disks:
def __init__(self,randomize=0): #constructor
if randomize: #if 1 do
self.array=self.Rand(0,7,4)
else :
self.array=[0,0,0,0]
def Rand(self,low,high,num):
a=[] #starting at low to hight [low,high)
for i in range(num):
x=random.random()
a.append(int(x*(high-low)+low))
a.reverse()
return a
def SetDisk(self,a):
self.array = a
def CheckBlackPeg(self,other):
black,sa,oa=0,self.array,other.array
for n in range(3,-1,-1):
if sa[n]== oa[n]:
black+=1
return black
def CheckWhitePeg(self,other):
white,sa=0,self.array
tempArray=other.array[:]
for i in range(4):
for j in range(4):
if sa[i]==tempArray[j]:
white+=1
tempArray[j]=9 # so we won't count that same color twice
black=self.CheckBlackPeg(other)
return white-black
#my display class
class guessFrame:
def __init__(self,frameNum,parentFrame,
fourGuessesArray,hidden):
#constructor
self.myGuessFrame=Frame(parentFrame,relief=GROOVE,
borderwidth=1)
self.black=0
self.white=0
self.frameNum=frameNum
self.guessArray=fourGuessesArray
self.hidden = hidden
def Draw(self):
self.myLabel = Label(self.myGuessFrame,
text=str(self.frameNum))
self.myLabel.grid(row=self.frameNum,column=1)#,sticky=NS)
print self.frameNum
self.myColorSquare=[None]*4
for i in range(4):
temp=Canvas(self.myGuessFrame,width = 25)
self.myColorSquare[i]=temp
myColorCodeArray=self.getGuessColor()
for i in range(4):#draw the guess color
mcs = self.myColorSquare[i]
mcs.create_rectangle(0,0,25,25,fill=myColorCodeArray[i])
mcs.grid(row=5+self.frameNum,column=i+2,sticky=NSEW)
#Check For feedback
userGuess=disks(0)
userGuess.SetDisk(self.guessArray)
hidden = self.hidden
blackFeedback=hidden.CheckBlackPeg(userGuess)
whiteFeedback=hidden.CheckWhitePeg(userGuess)
self.myFeedbackSquare=[None]*4
mfbs = self.myFeedbackSquare
for i in range(4):
mfbs[i] = Canvas(self.myGuessFrame,width = 25)
i=0
while blackFeedback > 0:
#black
fbs = mfbs[i]
fbs.create_rectangle(0,0,10,10,fill='#000000')
fbs.grid(row=5+self.frameNum,column=6+i)#,sticky=NSEW)
i+=1
blackFeedback-=1
while whiteFeedback > 0:
#White
fbs = mfbs[i]
fbs.create_rectangle(0,0,10,10,fill='#ffffff')
fbs.grid(row=5+self.frameNum,column=6+i)#,sticky=NSEW)
i+=1
whiteFeedback-=1
while i<4:
fbs = mfbs[i]
fbs.create_rectangle(0,0,10,10,fill='#808080')
fbs.grid(row=5+self.frameNum,column=6+i)#,sticky=NSEW)
i+=1
def getGuessColor(self):# return an Array of color for print out
sga = self.guessArray
guessColor=[9,9,9,9]
#black,cyan,green,magenta,red,white,yellow,gray
colors = ['#000000','#00ffff','#00ff00','#ff00ff','#ff0000',
'#ffffff','#ffff00',9,9,'#808080']
for i in range(4):
guessColor[i] = colors[sga[i]]
return guessColor
def test():
root=Tk()
mainFrame=Frame(root)
mainFrame.pack(fill = BOTH, expand = YES)
testingArrays=[[1,0,2,3],[4,5,6,9],[4,5,6,9]]
gfs=[]
h = 80
hidden=disks()
n = len(testingArrays)
for i in range(n):
gf=guessFrame(i+1,mainFrame,testingArrays[i],hidden)
gf.myGuessFrame.place(y=i*h,height=h)
gf.Draw()
gfs.append(gf)
root.geometry("%ix%i" %(n*h,n*h))
root.mainloop()
if __name__=='__main__':
test()
More information about the Python-list
mailing list