0

I'm a beginner in tkinter, so my question might be dumb. Eyes beware.

What I'm trying to do is use a combobox as an "entry" widget. The combobox is created in a function on a Toplevel window, and asks the user to choose a date among the different dates of the box. I have been trying to retrieve the selected value through a bind, but I keep bumping in scope issues when it comes to accessing the combobox ACTIVE value when the button is pressed.

def recupdate(event):
    global date = combobox.get(ACTIVE)

def datewindow():
    dwindow = tk.Toplevel()
    dwindow.title("Date Enquête")
    datetxt = Label(dwindow, text="Sélectionnez une date ou créez en une nouvelle")
    datetxt.pack(fill='x', padx=5, pady=5)
    listedate = read_bdd_date()
    btnselect = Button(dwindow, column=0, text="Sélectionner", command=(lbdate.get(lbdate.curselection(ACTIVE())))).pack(dwindow)
    combovar = StringVar()
    combobox = ttk.Combobox(dwindow, textvariable=combovar)
    combobox['values'] = listedate
    combobox['state'] = 'readonly'
    combobox.pack(fill='x', padx=5, pady=5)
    combobox.bind('<<ComboboxSelected>>', recupdate)

NB : the code above is most certainly wrong in some ways. I dont use the stringvar, among other things.

What I try to do here is retrieve the selected value and use it in a return so I can use it somewhere else.

Is is possible ? Should I use a Class to declare this toplevel window ? If yes, how ? Thank you all in advance.

  • Does this answer your question? [Why is the command bound to a Button or event executed when declared?](https://stackoverflow.com/questions/5767228/why-is-the-command-bound-to-a-button-or-event-executed-when-declared) – TheLizzard May 19 '21 at 13:48
  • This is not an exact duplicate but has a similar problem of "trying to use a local variable in same other place" [Using the variable from entry/button in another function in Tkinter](https://stackoverflow.com/questions/20771627/using-the-variable-from-entry-button-in-another-function-in-tkinter) – Lafexlos May 19 '21 at 13:49
  • Also what is `ACTIVE`? Is imported from `tkinter`? If so it is a string (`"active"`) so it isn't callable – TheLizzard May 19 '21 at 13:51
  • From what I learned on @TheLizzard answer, i can somehow retrieve the date with a .get(), but i still have to get out of my datewindow function with said value, while waiting for the user to actually select something. – Corcaedus May 19 '21 at 16:25
  • declare `combobox` as a `global` in the `datewindow()` function – Matiiss May 19 '21 at 16:47
  • How do you call `datewindow()`? – acw1668 May 20 '21 at 04:21
  • @acw1668 I call it like a function : it is called so the user can chose a date that is used afterwards in a databse insert process, but I can't find a way to retrieve said date from the function. – Corcaedus May 20 '21 at 05:30
  • What I mean is how the function be called? Via the `command` option of a button or called inside another function? – acw1668 May 20 '21 at 05:35
  • Inside another function. – Corcaedus May 20 '21 at 07:03
  • In that case, suggest to make `datewindow()` like a modal dialog (similar to `askopenfilename()`) and return `combovar.get()` after closing the window. – acw1668 May 20 '21 at 07:48

0 Answers0