-1

I am still learning Tkinter and I am trying to make a simple programe that on a button click will gen three random nums but the def will not change the var, it will print it to the console but will not change the vars

from tkinter import *
import random

firstNum = 0
secondNum = 0
thirdNum = 0

def PickNewNums():
    firstNum = random.randint(1, 100)
    secondNum = random.randint(1, 100)
    thirdNum = random.randint(1, 100)
    print(firstNum)
    return firstNum, secondNum, thirdNum

root = Tk()


mainTitle = Label (root, text = "Amazing Title")
newNumbers = Button(root, text = "Get New Numbers", command=PickNewNums)

firstNumber = Label(root, text = firstNum)
secondNumber = Label(root, text = secondNum)
thirdNumber = Label(root, text = thirdNum)

mainTitle.pack()
firstNumber.pack()
secondNumber.pack()
thirdNumber.pack()
newNumbers.pack(side = BOTTOM)
root.geometry("600x300")
root.mainloop()

Thanks for any help here guys!

Loki180
  • 73
  • 8
  • Briefly: you are creating new local variables rather than modifying the global variables. You are also `return`ing them to nowhere. Use `firstNumber.config(text=firstNum)` instead of `return`. – TigerhawkT3 Feb 22 '16 at 10:38

1 Answers1

0

I would prefer wrapping things in a class.

You can associate a Tkinter variable with a label. When the contents of the variable changes, the label is automatically updated:

v = StringVar()
Label(root, textvariable=v).pack()

To update do :

v.set("Hest!")
Svend Feldt
  • 758
  • 4
  • 17