-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombobox.py
41 lines (30 loc) · 964 Bytes
/
combobox.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import tkinter as tk
from tkinter import ttk
from tkinter.messagebox import showinfo
from calendar import month_name
window = tk.Tk()
# config the root window
window.geometry('300x200')
window.resizable(False, False)
window.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(window, 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)
window.mainloop()