Question

I have a piece of code that i have returned to and fixed so far. It's a math game that generates a question; asks the question; checks it and adds to the score. I am still somewhat of a beginner and have a question that i have seen many people have problems with. Although i still can't figure it out

I have an entry widget and want to get the info out of it but continue to get errors

Can someone what's wrong, where to put v = StringVar() and why it is wrong(I'd like to learn why)

This is a game that is a WIP and so please ignore other errors. import tkinter as tk import random as r

score = 0
a = 0
b = 0
answer = 0

def question_gen():
    global a
    global b
    global answer
    a = r.randint(0,100)
    b = r.randint(0,100)
    answer = (a+b)

def question_checker():
    global score
    user_input = v.get
    if user_input == answer:
        score += 1
    else:
        score += 0

class Demo1:
    def __init__(self, master):
        self.master = master
        self.frame = tk.Frame(self.master).pack()
        self.label = tk.Label(self.frame, text = 'Welcome To My Wonderful Math Game\n\nHow To Play\n\nWhen you pick a difficulty a new window will pop up\nYou have to answer to the question\nBefore the timer runs out\n\nGood Luck!').pack()
        self.button1 = tk.Button(self.frame, text = 'Easy', width = 25, command = self.new_window).pack()
        self.button2 = tk.Button(self.frame, text = 'Hard', width = 25, command = self.new_window).pack()

    def new_window(self):
        self.newWindow = tk.Toplevel(self.master)
        self.app = Demo2(self.newWindow)

class Demo2:
    def __init__(self, master):
        self.master = master
        self.frame = tk.Frame(self.master).pack()
        self.label = tk.Label(self.master, text = '{0} + {1} ='.format(a,b)).pack()
        self.entry = tk.Entry(self.master, textvariable=v).pack()
        self.quitButton = tk.Button(self.master, text = 'Quit', width = 25, command = self.close_windows).pack()
    def close_windows(self):
        self.master.destroy()

def main(): 
    root = tk.Tk()
    app = Demo1(root)
    root.mainloop()

if __name__ == '__main__':
    main()
Was it helpful?

Solution

where to put v = StringVar() ?

You have to store v at a place where you can reach it

  1. at entry widget instanciation
  2. at question checker

Possible solutions include

  • declare v in upper context, thus you can access it from anywhere
  • declare v as an attribute of demo2, pass it as an argument to question_checker (sounds better to me, Demo2 responsability is thus to show and check the answer of a question)
  • declare v as an attribute of demo1, pass it as an argument to Demo2 constructor and question_checker

By the way, if you do not make use of the trace functionality (react on each variable change) of StringVar, you can get rid of StringVar

class Demo2:
    def __init__(self, master):
        #[...]
        self.entry = tk.Entry(self.master)
        self.entry.pack()
        #[...]
    def close_windows(self):
        #access entry content through
        self.entry.get()

Also note that onelining widget creation an geometry in tkinter wrongly initialise your widget variables. It will always contain None since pack (and grid) always return None.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top