Python Forum
return a variable from a function - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: return a variable from a function (/thread-33237.html)



return a variable from a function - snakes - Apr-08-2021

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)

root.mainloop()



RE: return a variable from a function - jefsummers - Apr-08-2021

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.


RE: return a variable from a function - deanhystad - Apr-08-2021

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)
 
frame.mainloop()
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.


RE: return a variable from a function - snakes - Apr-09-2021

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",
                1:"Sunday",
                2:"Monday",
                3:"Tuesday",
                4:"Wednesday",
                5:"Thursday",
                6:"Friday"}
    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)
    else: 
        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)