-1

I have some problems with Tkinter, I want so retrieve the selected item of an option menu when pressing a button, but when testing the funcionality with a small testfuntion 'getdataset', I only get the predefined datavariable ('Birthdata') as an output so it seems that the datavariable.get() method is not returning the selected option in the optionmenu. I have looked everywhere but cannot seem to find the answer. Any help is appreciated.

Code with option menu

root = Tk()
root.configure(background='white')
def getdataset():
    print(datavariable.get())

datavariable = StringVar(root)
datavariable.set('Birthdata')
PickData = OptionMenu(root, datavariable,'Birthdata', 'Marriagedata', 'Deathdata',command=getdataset)
PickData.grid(column=1,columnspan=2,row=1)

Code to initialize test function with button click:

wordcloud = Button(root, text = 'Word Cloud', bg='white', width=20, height = 5, command=getdataset)

Output after multiple button clicks:

Birthdata
Birthdata
Birthdata

I w

K_Luijten
  • 3
  • 1
  • You could should be throwing an error. More specifically `TypeError: getdataset() takes 0 positional arguments but 1 was given`. Change `getdataset():` to `getdataset(_=None):` and let me know if that helps. – Mike - SMT Dec 17 '19 at 21:39
  • You should be getting an error, and the error will tell you what is wrong. The code posted will give an error if we fill in the missing pieces with obvious code. If you're not getting errors, please [edit] your question to include a proper [mcve]. – Bryan Oakley Dec 17 '19 at 21:42
  • @Mike-SMT I have tried it but it still only prints the predefined variable – K_Luijten Dec 17 '19 at 21:43
  • @BryanOakley Nope... I don't have any errors, which makes it weird and hard to solve – K_Luijten Dec 17 '19 at 21:44
  • You _must_ be getting errors, unless you're using a non-standard `OptionMenu`. The callback requires an argument, and your callback doesn't accept an argument. However, even with the error, your code works fine for me -- when I click the button it always shows the selected value. – Bryan Oakley Dec 17 '19 at 21:45

1 Answers1

1

You should be getting an error when selecting an option from your OptionMenu.

Specifically:

TypeError: getdataset() takes 0 positional arguments but 1 was given

To fix that we can add a argument that will handle this. Update your function to look like this:

def getdataset(_=None):

The reason for this is due to how your 2 different commands are interacting with this function. The OptionMenu command is sending an argument to the function when called where the Button command is not. This is not obvious at first but if you write something like:

def getdataset(arg=None):
    print(arg)

You will see that when you press the Button it will print None and when you select something from the menu it will print the value of the selection.

You code should look something like this:

import tkinter as tk


def getdataset(_=None):
    print(datavariable.get())


root = tk.Tk()
root.configure(background='white')
datavariable = tk.StringVar(root)
datavariable.set('Birthdata')
tk.OptionMenu(root, datavariable, 'Birthdata', 'Marriagedata', 'Deathdata', command=getdataset).grid()
tk.Button(root, text='Word Cloud', bg='white', width=20, height=5, command=getdataset).grid()
root.mainloop()
Mike - SMT
  • 14,784
  • 4
  • 35
  • 79
  • Yep, this is literally how my code looks right now and it is still only printing 'Birthdata'. I added the '_=None' argument but it does not change anything. – K_Luijten Dec 17 '19 at 21:49
  • Can you maybe add a screenshot of your code. I know its bad practice for a question but I need to see if there is a mismatch somewhere. Or add a link to an image. – Mike - SMT Dec 17 '19 at 21:50
  • So I restarted everything and it is working now. Thanks for your help anyways! – K_Luijten Dec 17 '19 at 22:02