tkinter StringVar doesn't update

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sharroj
    New Member
    • Jun 2013
    • 1

    tkinter StringVar doesn't update

    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?
    Code:
    def endGameMessageTK(score):
        global root
        root=Tk()
        root.title('game over')
        mainframe = ttk.Frame(root, padding="3 3 12 12")
        mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
        scoreString=StringVar()
        scoreString.set(str(score))
        ttk.Label(mainframe, text='game over.  Your socore is: ').grid(column=1, row=1, sticky=(W, E))
        ttk.Button(mainframe, text="Quit", command=tkinterQuit).grid(column=2, row=2, sticky=W)
        ttk.Button(mainframe, text="Replay", command=tkinterReplay).grid(column=1, row=2, sticky=W)
        ttk.Label(mainframe, textvariable=scoreString).grid(column=2, row=1, sticky=(W, E))
        root.mainloop()
    Last edited by bvdet; Jun 17 '13, 03:00 PM. Reason: Please use code tags when posting code [code]....[/code]
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    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:
    Code:
    try:
        import Tkinter as tk     ## Python 2.x
    except ImportError:
        import tkinter as tk     ## Python 3.x
    
    class TestIt():
        def __init__(self):
            self.root = tk.Tk()
            self.root.withdraw()
            self.ctr = 0
            self.start_game()
            self.root.mainloop()
    
        def another(self):
            self.top.destroy()
            self.start_game()
    
        def start_game(self):
            self.ctr += 1
            self.top = tk.Toplevel(self.root)
            self.top.geometry("200x75")
            self.top.title("Game # %d" % (self.ctr))
            self.top.grid()
            tk.Label(self.top, text="Game # %d" % (self.ctr)).grid()
            tk.Button(self.top, text="Start New Game",
                      command=self.another).grid()
            tk.Button(self.top, text="Quit",
                      command=self.root.quit).grid()
    
    TI=TestIt()

    Comment

    Working...