Bind a function to the event of selecting a combobox item [duplicate]
The reason why nothing happens is because simply selecting an item in the combobox does not trigger any events that does what you want. You need to bind
to <<ComboboxSelected>>
virtual event and create a function for it to do your specific task:
def func(event):
print(event.widget.get()) # event.widget refers to widget triggering the event, in this case combobox widget
city.bind('<<ComboboxSelected>>',func)
As I mentioned, you need to take a look at how GUI works and how python runs the code, so basically the code at your main block gets executed at run time. Apologies if you got the wrong idea before :)
Edit:
import sqlite3
from tkinter import ttk
import tkinter as tk
root = tk.Tk()
root.geometry('400x400')
root.config(bg="gray")
root.state("normal")
conn = sqlite3.connect('database')
cursor = conn.cursor()
def combo_city(event=None):
cursor.execute('SELECT City FROM Nations')
values = [row[0] for row in cursor]
return values
def func(event):
print(event.widget.get()) # event.widget refers to widget
city=ttk.Combobox(width = 25)
city.place(x=10, y=10)
city.set("Name")
city['value'] = combo_city()
city.bind('<<ComboboxSelected>>',func) #update
root.mainloop()