Tkinter help, please...

John McMonagle jmcmonagle at velseis.com.au
Thu May 24 18:50:27 EDT 2007


I've made a couple of minor changes to your code from the Cribbage class 
down:

class Cribbage:
      def __init__(self, win):

          self.parent = win    # <---- make the toplevel Tk window an
                               # <---- attribute of the class
          #Draw the interface
          self.f = Frame(self.parent)
          self.f.grid()
          self.cardpix = [Label(self.f), Label(self.f), Label(self.f), 
Label(self.f), Label(self.f)]
          clr = ["red", "blue"]
          n = 1
          for c in self.cardpix:
              c.configure(width=10, height=10, bg=clr[n%2], text="card 
"+str(n))
              c.grid(row=0, column=n-1)
              n += 1
          self.scorebox = Label(self.f)
          self.scorebox.configure(height=5, bg="green", text="Score: 0")
          self.scorebox.grid(row=1, column=0, columnspan=5)

      def play(self):
          d = Deck()
          hand = [d.deal(), d.deal(), d.deal(), d.deal()]
          flipped = d.deal()
          hand.append(flipped)
          hand.sort()
          score = tally(hand, flipped)
          self.scorebox.configure(text= "Score: " + str(score))
          #Eventually, display the actual card images, but for now...
          for c, x in zip(hand, self.cardpix):
              x.configure(text = str(c))
              #I've tried both of these, to no avail.
              self.parent.update()    <---- update the toplevel window
          return score

def main():
      win = Tk()
      run = Cribbage(win)
      score = 0
      best = 0
      while score < 24:
          score = run.play()
          if score >= best: best = score
          time.sleep(1)      <--- short sleep to see what's happening
      win.mainloop()

main()



Regards,

John



More information about the Python-list mailing list