Python Forum
return a variable from a function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
return a variable from a function
I'm new to Python and coding and after learning the basics I made a "day of the week from date" calculator. It works well.
Now I'm trying to make a gui for it. I'm using tkinter but have fallen at almost the first hurdle. I hope someone can help!?

I've spent two days on this simple task!
I want the user to choose a date, month and year and for each one to be saved as a variable. I'm trying to use Combobox for the selection.

I kind of know whats happening... I'm trying to define q before the user has selected an option from the combobox.
I've added the chosen_day label at the end just to check that q is set. I've tried everything (almost)! Maybe I shouldn't be using a function?

Here is an example... I want the user to choose a date between 1st and 31st and for it to be saved as variable "q".

Thanks for any help
def selected_day(event):
    q = combo_day.get()
    return q

combo_day = Combobox(frame)
combo_day['values'] = list (range(1,32))
combo_day.set("Choose a day")
combo_day.grid(row=0, column=0)
combo_day.bind("<<ComboboxSelected>>", selected_day)

q = selected_day()

chosen_day = Label(frame, text=q).grid(row=1, column=0)

I'm not a Tkinter person but will comment on the structure -

With a GUI interface, actions are precipitated by events triggered by the user. So, you do not want the q=selected_day() line. You must trust that the user will click the combo_day and that will trigger the function. The result returned by the function then needs to be stored somewhere. When I write in WxPython, I will have a class structure, put the function in that, and have the values I am keeping stored in that class. Will defer to those who use Tkinter more on that part.
import tkinter as tk
from tkinter import ttk as ttk

def selected_day(_):
   chosen_day['text'] = combo_day.get()

frame = tk.Tk()

chosen_day = tk.Label(frame, text='', width=12)
chosen_day.grid(row=1, column=0, padx=5, pady=5)

combo_day = ttk.Combobox(frame, width=12, values=list(range(1, 32)))
combo_day.grid(row=0, column=0, padx=5, pady=5)
combo_day.set("Choose a day")
combo_day.bind("<<ComboboxSelected>>", selected_day)
Bind the combo box selected event to a function that updates the label. The label then gets updated when the event occurs. This is the fundamental difference between writing a console based application and a GUI application. In a GUI the order and timing of events are dictated by the user, not by the order of statements in the code.
I managed get it working, but apparently not in a way that is recommended. I simply added "global q" to the function which meant I didn't need the "return q".

There are 8 functions altogether many of which needed to provide a variable for use in the final calculation. It works well but my next task is to work out how to do it without using "global". I've got a few niggles to iron out and a few tweaks here and there first though. Overall, it's been fun!

Thank you for you help

def selected_day(event):
    global q
    q = int(combo_day.get())

combo_day = Combobox(frame)
combo_day['values'] = list (range(1,32))
combo_day.set("Choose a day")
combo_day.grid(row=2, column=0)
combo_day.bind("<<ComboboxSelected>>", selected_day)
Final calculation
def result():
    day_code={  0:"Saturday",
    if year_input == 1752 and m == 9 and q <3:
        day = day_code[(q + 13*(m+1)//5 + k + k//4 + 5 - j) %7]
        print_result = Label(frame, text=day).grid(row=4, column=0)
        day = day_code[(q + 13*(m+1)//5 + k + k//4 + j//4 - 2*j) %7]
        print_result = Label(frame, text=day).grid(row=4, column=0)

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Passing variable to function. KDog 2 449 May-25-2021, 09:15 PM
Last Post: KDog
  tkinter -- after() method and return from function -- (python 3) Nick_tkinter 12 1,219 Feb-20-2021, 10:26 PM
Last Post: Nick_tkinter
  Call local variable of previous function from another function with Python3 & tkinter Hannibal 5 1,221 Oct-12-2020, 09:16 PM
Last Post: deanhystad
  Global Variable in Event Function? What am I doing wrong here? p_hobbs 1 992 Nov-13-2019, 02:50 PM
Last Post: Denni
  [WxPython] How to return a variable from an event handler Shaba1 2 2,120 Jun-13-2019, 02:47 PM
Last Post: Shaba1
  Unable to return value from callback function of a button in Python Tkinter nilaybnrj 4 16,116 Aug-05-2018, 11:01 PM
Last Post: woooee

Forum Jump:

User Panel Messages

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