Files
004_comission/daniel_jo/ITP4459_assignment_2023/tests/test_combobox.py
louiscklaw 5ea2c37f8d update,
2025-01-31 19:38:17 +08:00

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()