How to add multiple choices with tkinter ttk?

Viewed 79
import os
import tkinter as tk
from tkinter import *
from tkinter.ttk import *
root = Tk()
variable = StringVar(root)
variable = StringVar(root)
variable.set('GB')
w = Combobox(root, values = choices)
w.pack(); root.mainloop()
choices = ['']
msedgelocation= 'my msedge location'
chromelocation= 'my chrome location'
if os.path.exists(msedgelocation):
# add msedge to choices
else:
if os.path.exists(chromelocation):
# add chrome to choices

This will only add 1 option, How do I change the script so it adds multiple options? Preferably just code and an explanation, but anything helps. Thanks!

EDIT: Also a submit box would be nice

1 Answers

I improved Your code a bit:

from tkinter import Tk, StringVar
from tkinter.ttk import Combobox
import os


def get_choice(event):
    os.startfile(choices[event.widget.get()])


root = Tk()

variable = StringVar()
variable.set('GB')

choices = {'GB': 'gb.exe', 'AUS': 'aus.exe', 'USA': 'usa.exe', 'Chrome': 'chrome.exe'}


c_box = Combobox(root, values=list(choices.keys()), textvariable=variable, state='readonly')
c_box.pack()

c_box.bind('<<ComboboxSelected>>', get_choice)

root.mainloop()

First of I got all the info about the combobox here where it was put in simple words.

The other thing is that You should name variables with meaningful variable names not just one letter names, especially if You plan on using the variable.

Also as I mentioned in the comments, it is suggested that You import only what You need, as You can see in my provided code.

Lastly, as @acw1668 already mentioned in comments the code after .mainloop() will only get executed when the window closes.

Related