HighARc
HighARc

Reputation: 117

Tkinter option menu

Hi there I am currently learning to use Tkinter in python 2.7. I am writing a programs that uses user accounts to select a different set of rules. In my program I am trying to make it so the user can click; "New User"or "Existing User." The problem is that I am using the tkinter the syntax for the optionmenu is as follows.

    option = OptionMenu(master, var, "one", "two", "three", "four")
    option.pack()

As seen the "One","two", and "three" are all options in the list, which I would like to replace with user names. The question is how do I take a mutable list of usernames, and input said list into this option field? I have tried using a tuple but to mo avail.. someone help me please!

Upvotes: 3

Views: 1676

Answers (1)

John Meikle
John Meikle

Reputation: 121

You could use the *-operator to unpack an argument list like so:

...
options = ['one', 'two', 'three', 'four']
option = OptionMenu(master, var, *options)
option.pack()
...

More information about unpacking arguments can be found here: http://docs.python.org/2/tutorial/controlflow.html#unpacking-argument-lists

You should note that the *-operator can also be used to accept an arbitrary number of arguments, for example:

def print_names(*names):
    for name in names:
        print(name)

print_names('Bob', 'James', 'Harry')

Which would output:

Bob
James
Harry

There also exists the **-operator which works in a very similar way, except that it uses keyword arguments. If you wish to learn more about these operators and their usages, then I highly recommend visiting the documentation article that I linked above (relevant information begins here: http://docs.python.org/2/tutorial/controlflow.html#keyword-arguments).

Upvotes: 4

Related Questions