Reputation: 23
I copied some code from online:
import tkinter as tk
from tkinter import *
root = tk.Tk()
root.title('Holiday Finder')
root.geometry('600x400+50+50')
root.resizable(False, False)
root.attributes('-topmost', 1)
def show():
label.config( text = clicked.get() )
options = [
"Afghanistan",
"Albania",
"Algeria",
"Andorra",
"Angola",
"Antigua and Barbuda",
"Argentina",
"Armenia",
"Australia",
"Austria",
"Azerbaijan",
"Bahamas",
"Bahrain",
"Bangladesh",
"Barbados",
"Belarus",
"Belgium",
"Belize",
"Benin",
"Bhutan",
"Bolivia",
"Bosnia and Herzegovina",
"Botswana",
"Brazil",
"Brunei",
"Bulgaria",
"Burkina Faso",
"Burundi",
"Cabo Verde",
"Cambodia",
"Cameroon",
"Canada",
"Central African Republic",
"Chad",
"Chile",
"China",
"Colombia",
"Republic of the Congo",
"Costa Rica",
"Cote d'Ivoire",
"Croatia",
"Cuba",
"Cyprus",
"Czech Republic"
]
clicked = StringVar()
clicked.set("Afghanistan")
drop = OptionMenu(root, clicked, *options)
drop.pack()
button = Button(root, text="Find Websites", command=show).pack()
label = Label(root, text=" ")
label.pack()
root.mainloop()
But when you clicked the button the country name appears (which is what the code is supposed to do):
However I am trying to make it so when you click a country it says something different for each one (e.g. Afghanistan = 1
, Albania = 2
etc.)
Upvotes: 0
Views: 613
Reputation: 41862
If you actually want the position of the country in your list, you can simply change show()
to read:
def show():
label.config(text=options.index(clicked.get()))
If you want something more arbitrary, then using a dict
comes to mind:
from tkinter import *
def show():
label.config(text=options[clicked.get()])
options = {
'Alabama': 'AL',
'Alaska': 'AK',
'Arizona': 'AZ',
'Arkansas': 'AR',
'California': 'CA',
'Colorado': 'CO',
'Connecticut': 'CT',
'Delaware': 'DE',
# ...
}
root = Tk()
root.title('Holiday Finder')
root.geometry('600x400+50+50')
root.resizable(False, False)
root.attributes('-topmost', 1)
clicked = StringVar()
clicked.set('Alabama')
drop = OptionMenu(root, clicked, *options.keys())
drop.pack()
Button(root, text="Find Websites", command=show).pack()
label = Label(root, text=" ")
label.pack()
root.mainloop()
Upvotes: 1