Python Forum
[Tkinter] Passing on values
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Passing on values
#1
Hi, I'm new to Python and tkinter programming. I have tried to find an answer to my problem on this forum, but couldn't find it. This problem is surely a most common problem, but still I am not able to solve it! Cry

I am trying to pass on variables that contain choices made with dropdown menu's, radio buttons and so on. Every time I try this I get "0" (or "PY_VAR0" if I didn't put "choice = 0 " at the beginning of the script) as a result.

So I tried a simple script to bring things back to the essence but with the same result. My code:

from tkinter import *

master = Tk()

choice = IntVar()
choice = 0

def callback1():
    print ("Your choice is number 1!")
    choice = 1
    
def callback2():
    print ("Your choice is number 2!")
    choice = 2
    
def callback3():
    print ("Your choice is number 3!")
    choice = 3


def callback6():
    print ("Your final choice is: ")
    print (choice)
    

b = Button(master, text="Choice 1", command=callback1)
b.grid()

b = Button(master, text="Choice 2", command=callback2)
b.grid()

b = Button(master, text="Choice 3", command=callback3)
b.grid()


b = Button(master, text="Let's show what choice you made!", command=callback6)
b.grid()

master.mainloop() 
When I make a choice: say "Choice 2", when I press the last button I expect my choice to be "2". Instead it is always "0", no matter what choice I made before.

What am I doing wrong?
Reply


Messages In This Thread
Passing on values - by DurkVell - Oct-24-2020, 07:22 PM
RE: Passing on values - by Larz60+ - Oct-24-2020, 07:23 PM
RE: Passing on values - by deanhystad - Oct-25-2020, 04:04 AM
RE: Passing on values - by DurkVell - Oct-25-2020, 10:33 AM
RE: Passing on values - by pnachtwey - Oct-25-2020, 05:26 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020