Home > Enterprise >  putting dictionary to listbox whenever clicking the radio button
putting dictionary to listbox whenever clicking the radio button

Time:06-06

It's my first time here in stack overflow and I would like to learn how to get the contents of the continents dictionary and put it on the listbox that I created whenever I click the radio button, I would like to learn new things by asking other people that are knowledgeable than me thank you so much.

#Dictionaries
continents = {
"Asia":["Philippines","Malaysia","Taiwan","Singapore","Thailand","India"],

"Africa":["Algeria","Angola","Egypt","Cape Verde","Liberia","Kenya","Morocco","Nigeria"],

"America":["Venezuela","Peru","Jamaica","United States","Cuba","Chile","Argentina"],

"Europe":["Russia","Germany","United Kingdom","Italy", "Ukraine","France","Belgium"]
}


#Frame One

var = IntVar()

Radiobutton(frame_1, text="Asia", font=("Times",12,"bold"), fg="blue", variable=var, value=1, borderwidth= 1, relief = SOLID,width = 10, command=lambda: Asia_click(var.get())).pack(side = LEFT)
Radiobutton(frame_1, text="Africa", font=("Times",12,"bold"), fg="blue", variable=var, value=2, borderwidth= 1, relief = SOLID,width = 11, command=lambda: Africa_click(var.get())).pack(side = LEFT)
Radiobutton(frame_1, text="America", font=("Times",12,"bold"), fg="blue", variable=var, value=3, borderwidth= 1, relief = SOLID,width = 11, command=lambda: America_click(var.get())).pack(side = LEFT)
Radiobutton(frame_1, text="Europe", font=("Times",12,"bold"), fg="blue", variable=var, value=4, borderwidth= 1, relief = SOLID,width = 11, command=lambda: Europe_click(var.get())).pack(side = LEFT)

#Frame Two

#Listbox and Scrollbar 
flag_listbox = Listbox(frame_2)
flag_listbox.pack(side = LEFT, fill = BOTH)

flag_scrollbar = Scrollbar(frame_2)
flag_scrollbar.pack(side = RIGHT, fill = BOTH)

flag_listbox.config(yscrollcommand = flag_scrollbar.set)

flag_scrollbar.config(command = flag_listbox.yview)


#Radio Button click 
def Asia_click(Value):
    asia_label = Label(root, text=Value)
    asia_label.grid()        

def Africa_click(Value):
    africa_label = Label(root, text=Value)
    africa_label.grid()
    
def America_click(Value):
    america_label = Label(root, text=Value)
    america_label.grid()
    
def Europe_click(Value):
    europe_label = Label(root, text=Value)
    europe_label.grid()
    

here is my sample output

CodePudding user response:

I made little changes to code and the code is

from tkinter import *

# Dictionaries
root = Tk()

frame_1 = Frame(root)
frame_1.pack(expand=TRUE, fill='both', pady=1)
frame_2 = Frame(root)
frame_2.pack(expand=TRUE, fill='both',pady=1)

continents = {
    "Asia": ["Philippines", "Malaysia", "Taiwan", "Singapore", "Thailand", "India"],

    "Africa": ["Algeria", "Angola", "Egypt", "Cape Verde", "Liberia", "Kenya", "Morocco", "Nigeria"],

    "America": ["Venezuela", "Peru", "Jamaica", "United States", "Cuba", "Chile", "Argentina"],

    "Europe": ["Russia", "Germany", "United Kingdom", "Italy", "Ukraine", "France", "Belgium"]
}

# Frame One

var = StringVar()
var.set('Select')

Radiobutton(frame_1, text="Asia", font=("Times", 12, "bold"), fg="blue", variable=var, value='Asia', borderwidth=1,
            relief=SOLID, width=10, command=lambda: RadioSelected(var.get())).pack(side=LEFT)
Radiobutton(frame_1, text="Africa", font=("Times", 12, "bold"), fg="blue", variable=var, value="Africa", borderwidth=1,
            relief=SOLID, width=11, command=lambda: RadioSelected(var.get())).pack(side=LEFT)
Radiobutton(frame_1, text="America", font=("Times", 12, "bold"), fg="blue", variable=var, value="America", borderwidth=1,
            relief=SOLID, width=11, command=lambda: RadioSelected(var.get())).pack(side=LEFT)
Radiobutton(frame_1, text="Europe", font=("Times", 12, "bold"), fg="blue", variable=var, value="Europe", borderwidth=1,
            relief=SOLID, width=11, command=lambda: RadioSelected(var.get())).pack(side=LEFT)

# Frame Two

# Listbox and Scrollbar
flag_listbox = Listbox(frame_2)
flag_listbox.pack(side=LEFT, fill=BOTH)

flag_scrollbar = Scrollbar(frame_2)
flag_scrollbar.pack(side=RIGHT, fill=BOTH)

flag_listbox.config(yscrollcommand=flag_scrollbar.set)

flag_scrollbar.config(command=flag_listbox.yview)


# Radio Button click
def RadioSelected(Value):
    flag_listbox.delete(0, END)
    list_values = continents[Value]
    for i in list_values:
        flag_listbox.insert(END, i)


root.mainloop()

the values are changes when u clicked the radio button

  • Related