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
#1
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()
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,512 Jul-06-2022, 10:19 PM
Last Post: Extra
  [Tkinter] Passing variable to function. KDog 2 2,166 May-25-2021, 09:15 PM
Last Post: KDog
  tkinter -- after() method and return from function -- (python 3) Nick_tkinter 12 7,450 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,452 Oct-12-2020, 09:16 PM
Last Post: deanhystad
  Global Variable in Event Function? What am I doing wrong here? p_hobbs 1 3,501 Nov-13-2019, 02:50 PM
Last Post: Denni
  [WxPython] How to return a variable from an event handler Shaba1 2 3,945 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,811 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