FAQ
HI,
I am new to python and tkinter, I have some question on tkinter and
canvas. I am using tkinter to build my GUI, I need to be able to
display some colored square on the screen, I can get everything to
work fine except my frame size. the frame contain 6 square box(canvas
object) and one label. The colored square looks just like the size
that I wants, but in fact it take up so much more room under the
square to store it. (I noticed that when I change the gird mathod
which I am currently using to the pack method and packing them one on
top of each other, I see a lot of space in between every square). does
anybody has any idea of how I can fix that? codes are on the bottom of
the page

second question:
if I have a frame inside a frame. I want to put some object in the
inside-frame using the grid method, when I say the row and column,
would I be using the row and column for the inside-frame or the
outside-frame. I guess my question is, how many measuring system are
we dealing with?

third question:
if I am using the grid method, does every row and column has to be the
same size?

Thank you very much
Benson

code:
----------------------------------------------
import random
import copy
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()
x=x*(high-low)+low
x=int(x)
a=[x,]+a
return a[:]

def SetDisk(self,a=0,b=0,c=0,d=0):
self.array[0]=a
self.array[1]=b
self.array[2]=c
self.array[3]=d

def CheckBlackPeg(self,other):
n=3
black=0
while n>=0:
if self.array[n]== other.array[n]:
black=black+1
n=n-1
return black

def CheckWhitePeg(self,other):
i=0
j=0
white=0
tempArray=other.array[:]
while i<4:
while j<4:
if self.array[i]==tempArray[j]:
white=white+1
tempArray[j]=9 # so we won't count that same color twice
j=j+1
break
j=j+1
j=0
i=i+1
black=self.CheckBlackPeg(other)
return white-black


#my display class
class guessFrame:
def __init__(self,frameNum,parentFrame,fourGuessesArray):
#constructor
self.myGuessFrame=Frame(parentFrame,relief=GROOVE,borderwidth=1)
#self.myGuessFrame.geometry("30*20")
#self.myGuessFrame=parentFrame
self.black=0
self.white=0
self.frameNum=frameNum
self.guessArray=fourGuessesArray

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.myLabel.grid(row=5+self.frameNum,column=1)
self.myColorSquare=[None]*4
for i in range(4):
tempÊnvas(self.myGuessFrame,width = 25)
self.myColorSquare[i]=temp
i=0
myColorCodeArray=self.getGuessColor()
for i in range(4):#draw the guess color
self.myColorSquare[i].create_rectangle(0,0,25,25,

fill=myColorCodeArray[i])
self.myColorSquare[i].grid(row=5+self.frameNum,
column=i+2,sticky=NSEW)
i=0


#Check For feedback
userGuess=disks(0)
userGuess.SetDisk(self.guessArray[0],self.guessArray[1],
self.guessArray[2],self.guessArray[3])
blackFeedback=computerHidden.CheckBlackPeg(userGuess)
whiteFeedback=computerHidden.CheckWhitePeg(userGuess)
self.myFeedbackSquare=[None]*4
for i in range(4):
tempÊnvas(self.myGuessFrame,width = 25)
self.myFeedbackSquare[i]=temp
i=0

feedbackCol=6
i=0
while blackFeedback > 0:
#black
self.myFeedbackSquare[i].create_rectangle(0,0,10,10,fill='#000000')
self.myFeedbackSquare[i].grid(row=5+self.frameNum,
column=6+i)#,sticky=NSEW)
i=i+1
blackFeedback=blackFeedback-1
while whiteFeedback > 0:
#White
self.myFeedbackSquare[i].create_rectangle(0,0,10,10,fill='#ffffff')
self.myFeedbackSquare[i].grid(row=5+self.frameNum,
column=6+i)#,sticky=NSEW)
i=i+1
whiteFeedback=whiteFeedback-1
while i<4:
self.myFeedbackSquare[i].create_rectangle(0,0,10,10,fill='#808080')
self.myFeedbackSquare[i].grid(row=5+self.frameNum,
column=6+i)#,sticky=NSEW)
i=i+1
self.myGuessFrame.grid(row=self.frameNum,column=1,columnspan=9)

def getGuessColor(self):# return an Array of color for print out
i=0
guessColor=[9,9,9,9]
while i<4:
if(self.guessArray[i]==0):
guessColor[i]='#000000' #black
elif(self.guessArray[i]==1):
guessColor[i]='#00ffff' #cyan
elif(self.guessArray[i]==2):
guessColor[i]='#00ff00' #Green
elif(self.guessArray[i]==3):
guessColor[i]='#ff00ff' #Magenta
elif(self.guessArray[i]==4):
guessColor[i]='#ff0000' #Red
elif(self.guessArray[i]==5):
guessColor[i]='#ffffff' #White
elif(self.guessArray[i]==6):
guessColor[i]='#ffff00' #yellow
elif(self.guessArray[i]==9):
guessColor[i]='#808080' #Gray, our background color
i=i+1

return guessColor[:]
#-----------------------------------------

root=Tk()
testingArray=[1,0,2,3]
computerHidden=disks()
mainFrame=Frame(root,width0,height0)
mainDisplayFrame1=guessFrame(1,mainFrame,testingArray)
mainDisplayFrame1.Draw()
testingArray=[4,5,6,9]
mainDisplayFrame2=guessFrame(2,mainFrame,testingArray)
mainDisplayFrame2.Draw()
mainDisplayFrame3=guessFrame(3,mainFrame,testingArray)
mainDisplayFrame3.Draw()

mainFrame.pack()
root.mainloop()

Search Discussions

  • Anton Vredegoor at Nov 18, 2002 at 1:40 pm
    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()

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedNov 17, '02 at 10:27p
activeNov 18, '02 at 1:40p
posts2
users2
websitepython.org

2 users in discussion

Anton Vredegoor: 1 post Benson Ngai: 1 post

People

Translate

site design / logo © 2022 Grokbase