Tkinter/Canvas question

Benson Ngai benson123456 at yahoo.com
Sun Nov 17 17:27:15 EST 2002


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=Canvas(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=Canvas(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,width=30,height=30)
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()



More information about the Python-list mailing list