42 lines
952 B
Python
42 lines
952 B
Python
import tkinter as tk
|
|
from tkinter import ttk
|
|
from tkinter.messagebox import showinfo
|
|
from calendar import month_name
|
|
|
|
root = tk.Tk()
|
|
|
|
# config the root window
|
|
root.geometry('300x200')
|
|
root.resizable(False, False)
|
|
root.title('Combobox Widget')
|
|
|
|
# label
|
|
label = ttk.Label(text="Please select a month:")
|
|
label.pack(fill=tk.X, padx=5, pady=5)
|
|
|
|
# create a combobox
|
|
selected_month = tk.StringVar()
|
|
month_cb = ttk.Combobox(root, textvariable=selected_month)
|
|
|
|
# get first 3 letters of every month name
|
|
month_cb['values'] = [month_name[m][0:3] for m in range(1, 13)]
|
|
|
|
# prevent typing a value
|
|
month_cb['state'] = 'readonly'
|
|
|
|
# place the widget
|
|
month_cb.pack(fill=tk.X, padx=5, pady=5)
|
|
|
|
|
|
# bind the selected value changes
|
|
def month_changed(event):
|
|
""" handle the month changed event """
|
|
showinfo(
|
|
title='Result',
|
|
message=f'You selected {selected_month.get()}!'
|
|
)
|
|
|
|
month_cb.bind('<<ComboboxSelected>>', month_changed)
|
|
|
|
root.mainloop()
|