#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    19
    Rep Power
    0

    Random question help


    Hello,
    I'm having this problem with random questions. This is what I am trying to do. In normal python 3.3 I have a math game. The code below is what the questions look like in normal 3.3 python.
    Code:
     import random
            Qa=random.randint(1, 10)
            Qab=random.randint(1, 10)
            print ('Question 1:   ' + str (Qa) +' + ' + str (Qab) + ' = ?') # separate the question number please!
            Qbaa=input()      # Qba is a string
            Qbaaa = Qa + Qab  # Qbaa is an integer
            if Qbaa == str(Qbaaa):   # compare as strings.   str(Qbaa)
                print ('Correct! + 25 to score!')
                Score = Score + 25
                print (Score)
            else:
                print ('Incorrect,  -25 to score!')
                Score = Score - 25
                print (Score)
    That is what I'm trying to put in my Tkinter code.
    Tkinter Code:
    Code:
            def callback5():
                        label = Label(root, textvariable=var, relief=RAISED)
                        var.set ("You are about to start the game!")
                        label.pack()
                        Qa=random.randint(1, 10)
                        Qab=random.randint(1, 10)
                        Qabb=Qa + Qab 
                        label = Label(root, textvariable=var, relief=RAISED)
                        var.set ("Question 1: " + Qa + " + " + Qab + " = ? ")
                        label.pack()
                        Qaanswer = input()
                        if Qaanswer == "str Qabb":
                        label = Label(root, textvariable=var, relief=RAISED)
                        var.set ("Correct!")
                        label.pack()
                            
                        
            b = Button(root, text="Play Game", command=callback5)
            b.pack()
            b = Button(root, text="Ranks", command=callback4)
            b.pack()
            b = Button(root, text="About the Game", command=callback3)
            b.pack()
    Thank you.
  2. #2
  3. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    620
    Rep Power
    65
    You keep creating the same label object.
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    19
    Rep Power
    0
    Originally Posted by Dietrich
    You keep creating the same label object.
    Well, sorry I guess I did not make my problem clear. I was wondering if
    Code:
    Qa = random.randint(1, 10)
    Works in Tkinter?
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,996
    Rep Power
    481
    Don't create the Tkinter.Label in the callback.
    Do create them in the main code, just once.
    In the callback change the value of the text variable.
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    19
    Rep Power
    0
    Originally Posted by b49P23TIvg
    Don't create the Tkinter.Label in the callback.
    Do create them in the main code, just once.
    In the callback change the value of the text variable.
    okay I will try that if I know what you mean.

IMN logo majestic logo threadwatch logo seochat tools logo