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
#4
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)
Reply


Messages In This Thread
return a variable from a function - by snakes - Apr-08-2021, 12:53 PM
RE: return a variable from a function - by snakes - Apr-09-2021, 06:47 PM

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

Forum Jump:

User Panel Messages

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