0

Thanks to furas I have the following code for ListBox:

import tkinter as tk

def on_button():
#     for i, var in enumerate(o_vars):
#         print('OptionMenu {}: {}'.format(i, var.get()))
#     print()

    print('ListBox:', l.curselection())
    for i in l.curselection():
        print('option:', OPTIONS[i])
    print()


# --- main ---

OPTIONS = ["Script 1","Script 2","Script 3","Script 4","Script 5"]

root = tk.Tk()

# --- Listbox ---

tk.Label(root, text='Listbox', bg='#aaa').pack(fill='x')

l = tk.Listbox(root, selectmode='multiple')
l.pack()
l.insert('end', *OPTIONS)

# --- others ---

b = tk.Button(root, text='OK', command=on_button)
b.pack(fill='x')

root.mainloop()

When I run it, it gives me the following pop-up (image shown below). I then make my selections.

enter image description here

This is where I am stuck... I want to say if user selected Script2 print 'script 2'. If user selected script 5, print 'script 5'.

Below is the code I tried but it errored out:

if l.curselection() == 'Script1':
    print ('test')
if l.curselection() == 'Script2':
    print ('test2')

TclError: invalid command name ".92911768"

Also, how do I add a "Quit" button below "OK"?

*Any help is greatly appreciated

7
  • when you close dropdown menu then this widget can display only one option - and this is why you can select only one option. Use Listbox if you have to select more options at the same time. Commented Jan 13, 2017 at 8:29
  • BTW: we use CamelCase names for classes - ie. SimpleFormApp to make code more readable - PEP 8 -- Style Guide for Python Code Commented Jan 13, 2017 at 8:32
  • NEVER CHANGE TEXT OF QUESTION !!! - add new information below. Now you question is useless fo other users. Commented Jan 13, 2017 at 16:22
  • If you have new problem then create new question !!! Commented Jan 13, 2017 at 16:23
  • old method for problems - use print() to see what you have in variables and which part of code is executed. So use print(l.curselection()) to see what you get. Commented Jan 13, 2017 at 16:24

1 Answer 1

1

OptionMenu after closing dropdown menu can display only one option - so it can't select more options.

So you can use one of this method:

Only with many OptionMenu you can select in which order execute scripts.

Example shows all menthods in one window.

enter image description here

import tkinter as tk

# --- functions ---

def on_button():
    for i, var in enumerate(o_vars):
        print('OptionMenu {}: {}'.format(i, var.get()))
    print()

    print('ListBox:', l.curselection())
    for i in l.curselection():
        print('option:', OPTIONS[i])
    print()

    print('ChecboxBox:')
    for i, var in enumerate(cb_vars):
        if var.get():
            print('option:', OPTIONS[i])

# --- main ---

OPTIONS = ["Script 1","Script 2","Script 3","Script 4","Script 5"]

root = tk.Tk()

# --- OptionMenu ---

tk.Label(root, text='OptionMenus', bg='#aaa').pack(fill='x')

o_vars = []

for i in range(3):
    var = tk.StringVar(value='- select -')
    o_vars.append(var)
    o = tk.OptionMenu(root, var, *OPTIONS)
    o.pack()

# --- Listbox ---

tk.Label(root, text='Listbox', bg='#aaa').pack(fill='x')

l = tk.Listbox(root, selectmode='multiple')
l.pack()
l.insert('end', *OPTIONS)

# --- Checkbuttons ---

tk.Label(root, text='Checkbuttons', bg='#aaa').pack(fill='x')

cb_vars = []
for x in OPTIONS:
    var = tk.BooleanVar(value=False)
    cb_vars.append(var)
    c = tk.Checkbutton(root, text=x, variable=var)
    c.pack()

# --- others ---

b = tk.Button(root, text='OK', command=on_button)
b.pack(fill='x')

root.mainloop()

Result:

OptionMenu 1: Script 1
OptionMenu 2: Script 3
OptionMenu 3: Script 5

ListBox: (0, 2, 4)
option: Script 1
option: Script 3
option: Script 5

ChecboxBox:
option: Script 1
option: Script 3
option: Script 5

GitHub: furas/python-examples/tkinter/checkbutton-listbox-optionmenu

Sign up to request clarification or add additional context in comments.

4 Comments

thank you! I'm a little confused on the following step though. let's say I want to use the Listbox option for drop down menu and I select Script 1 and Script 2.... How do I code up the following: if selected option is Script 1 print "script 1", if selected option is script 2 print "script 2"
you can use Listbox instead of OptionMenu. l.curselection() gives you numbers of selected rows in listbox and you have to use it to get text from OPTIONS - you can see it in function on_button() and in result as ListBox: (0, 2, 4)
I apologize I'm very new to tkinter and classes ... I don't quite understand what you mean above
use Listbox instead of OptionMenu. Click elements on this listbox - you can select many elements. Then click button to run function on_button. Inside function you can get indexes of selected elements on listbox using curselection() - for selection on image it gives values (0, 2, 4). And then you can use this indexes to get text from list OPTIONS - OPTIONS[0] gives text Script 1, OPTIONS[2] gives text Script 3, OPTIONS[4] gives text Script 5.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.