0

I've created a tiny script using tkinter to get values from two dropdowns. One for selecting year and the other for selcting month. The script appears to be working fine as long as I print the values of the dropdowns within the respective functions.

I wish to print the result just before the mainloop where I've defined the print statement.

from tkinter import *

master = Tk()
master.geometry("200x200")
master.minsize(200, 200)
master.maxsize(200, 200)

options_one = ['2019''2020','2021']
options_two = ["Jan","Feb","Mar"]

var_one = StringVar(master)
var_one.set('Year')
OptionMenu(master, var_one, *options_one).pack()

var_two = StringVar(master)
var_two.set('Month')
OptionMenu(master, var_two, *options_two).pack()

def choose_year():
    year_name = var_one.get()
    print(year_name)

def choose_month():
    month_name = var_two.get()
    print(month_name)

Button(master,command=choose_year).pack()
Button(master,command=choose_month).pack()

print(f"It was year {year_name} and the month was {month_name}")
mainloop()
SMTH
  • 67
  • 1
  • 4
  • 17
  • 3
    Your "just before mainloop" point occurs *during the startup of your program* - it is not physically possible for the user to have made any selections yet! Anything that requires user input MUST happen in an event handler of some sort, such as a Button's `command=` function. – jasonharper Jan 30 '22 at 07:17
  • There are two requirements to get information out of a function: 1) it must have *been called*; 2) it must have actually done something to communicate the information back - either `return`ing it, modifying some passed-in object, or setting a global. Neither of those happens here. I suggest studying more fundamentals of working with functions in general, before trying to work with `tkinter`. Graphical programs require a more complex way of thinking that builds upon that foundation. – Karl Knechtel Jan 30 '22 at 07:50
  • I could learn from [this post](https://stackoverflow.com/questions/13099908/python-tkinter-return-value-from-function-used-in-command) that returning value from a function used in command is not possible, so I didn't follow that route @Karl Knechtel. – SMTH Jan 30 '22 at 08:01
  • How about doing [this](https://pastebin.com/vRPGrR08) instead! – MITHU Jan 30 '22 at 08:31
  • Yes; when the function is called *by clicking the button*, there isn't anywhere for the value to be returned *to*. So instead you do something that modifies the global state - for example, by changing the text of a Label. – Karl Knechtel Jan 30 '22 at 09:38

2 Answers2

2

Your variables have a local scope inside the functions they were created in.

You can use a class initialized outside the function calls to hold the selected values, or global variables which are generally frowned upon.

Charles
  • 31
  • 3
0

You can achieve your goal by making use of the global keyword. Use this:

from tkinter import *

master = Tk()
master.geometry("200x200")
master.minsize(200, 200)
master.maxsize(200, 200)

options_one = ['2019''2020','2021']
options_two = ["Jan","Feb","Mar"]

var_one = StringVar(master)
var_one.set('Year')
OptionMenu(master, var_one, *options_one).pack()

var_two = StringVar(master)
var_two.set('Month')
OptionMenu(master, var_two, *options_two).pack()

def choose_year():
    global year_name = var_one.get()
    print(year_name)

def choose_month():
    global month_name = var_two.get()
    print(month_name)

Button(master,command=choose_year).pack()
Button(master,command=choose_month).pack()

print(f"It was year {year_name} and the month was {month_name}")
mainloop()
acw1668
  • 40,144
  • 5
  • 22
  • 34
Babatunde Mustapha
  • 2,131
  • 20
  • 21