By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,668 Members | 1,404 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,668 IT Pros & Developers. It's quick & easy.

tkinter StringVar doesn't update

P: 1
The following is a function I have plugged into a game which displays a score passed to it and offers the options to play again or quit.

If replay is chosen, this tkinter window is closed (root.destroy) and the main program is run again

everything works but if I choose the option to replay, the next time the game ends, no value is shown for the score. The field is just left blank. The value is still being passed to the function, I checked that. Help?
Expand|Select|Wrap|Line Numbers
  1. def endGameMessageTK(score):
  2.     global root
  3.     root=Tk()
  4.     root.title('game over')
  5.     mainframe = ttk.Frame(root, padding="3 3 12 12")
  6.     mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
  7.     scoreString=StringVar()
  8.     scoreString.set(str(score))
  9.     ttk.Label(mainframe, text='game over.  Your socore is: ').grid(column=1, row=1, sticky=(W, E))
  10.     ttk.Button(mainframe, text="Quit", command=tkinterQuit).grid(column=2, row=2, sticky=W)
  11.     ttk.Button(mainframe, text="Replay", command=tkinterReplay).grid(column=1, row=2, sticky=W)
  12.     ttk.Label(mainframe, textvariable=scoreString).grid(column=2, row=1, sticky=(W, E))
  13.     root.mainloop()
Jun 17 '13 #1
Share this Question
Share on Google+
1 Reply

Expert 100+
P: 626
If replay is chosen, this tkinter window is closed (root.destroy)
It's difficult to tell from incomplete code snippets, but the problem is likely the root.destroy as that is the base Tkinter instance and it's never a good idea to destroy it. Instead use a Toplevel (withdraw the root if you don't want it to show) and destroy the Toplevel then create another; or just reset all the values to zero without destroying anything and start over. A simple example:
Expand|Select|Wrap|Line Numbers
  1. try:
  2.     import Tkinter as tk     ## Python 2.x
  3. except ImportError:
  4.     import tkinter as tk     ## Python 3.x
  5.  
  6. class TestIt():
  7.     def __init__(self):
  8.         self.root = tk.Tk()
  9.         self.root.withdraw()
  10.         self.ctr = 0
  11.         self.start_game()
  12.         self.root.mainloop()
  13.  
  14.     def another(self):
  15.         self.top.destroy()
  16.         self.start_game()
  17.  
  18.     def start_game(self):
  19.         self.ctr += 1
  20.         self.top = tk.Toplevel(self.root)
  21.         self.top.geometry("200x75")
  22.         self.top.title("Game # %d" % (self.ctr))
  23.         self.top.grid()
  24.         tk.Label(self.top, text="Game # %d" % (self.ctr)).grid()
  25.         tk.Button(self.top, text="Start New Game",
  26.                   command=self.another).grid()
  27.         tk.Button(self.top, text="Quit",
  28.                   command=self.root.quit).grid()
  29.  
  30. TI=TestIt() 
Jun 18 '13 #2

Post your reply

Sign in to post your reply or Sign up for a free account.