|
| 1 | +# ---------- TKINTER TUTORIAL 2 ---------- |
| 2 | + |
| 3 | +# Here I'll show how to use the widgets label, entry |
| 4 | +# button, check button, radio buttons and combo boxes |
| 5 | + |
| 6 | +# Get the standard library for Tk |
| 7 | +from tkinter import * |
| 8 | + |
| 9 | +# Get the newest widget themes from Tk 8.5 |
| 10 | +from tkinter import ttk |
| 11 | + |
| 12 | + |
| 13 | +def set_entry(*args): |
| 14 | + entry_1_txt.set("Hello") |
| 15 | + |
| 16 | + |
| 17 | +def chk_but_changed(*args): |
| 18 | + entry_1_txt.set(chk_but_1_txt.get()) |
| 19 | + |
| 20 | + |
| 21 | +def radio_changed(*args): |
| 22 | + entry_1_txt.set(radio_but_1_val.get()) |
| 23 | + |
| 24 | + |
| 25 | +def combo_changed(*args): |
| 26 | + entry_1_txt.set(combo_1_val.get()) |
| 27 | + |
| 28 | + |
| 29 | +root = Tk() |
| 30 | +root.title("Calculator") |
| 31 | + |
| 32 | +frame = ttk.Frame(root, padding="10 10 10 10") |
| 33 | + |
| 34 | +# ---------- GRID GEOMETRY MANAGER ---------- |
| 35 | +frame.grid(column=0, row=0, sticky=(N, W, E, S)) |
| 36 | +root.columnconfigure(0, weight=1) |
| 37 | +root.rowconfigure(0, weight=1) |
| 38 | + |
| 39 | +# ----- LABELS ----- |
| 40 | +# Used to get and set value of the label |
| 41 | +label_1_txt = StringVar() |
| 42 | +# Define the parent and text in the label |
| 43 | +label_1 = ttk.Label(frame, text='Data :') |
| 44 | +label_1.grid(column=1, row=1, sticky=(W, E)) |
| 45 | + |
| 46 | +# To change a value you must attach to the StringVar class |
| 47 | +label_1['textvariable'] = label_1_txt |
| 48 | +label_1_txt.set('Data ') |
| 49 | + |
| 50 | +# ----- ENTRY ----- |
| 51 | +# Used to get and set the value of the entry |
| 52 | +entry_1_txt = StringVar() |
| 53 | +# Define the number of characters long and the StringVar |
| 54 | +# it is associated with |
| 55 | +entry_1 = ttk.Entry(frame, width=7, textvariable=entry_1_txt) |
| 56 | +entry_1.grid(column=2, row=1, sticky=(W, E)) |
| 57 | + |
| 58 | +# You can get values by using get on a StringVar |
| 59 | +entry_1_txt.set(label_1_txt.get()) |
| 60 | + |
| 61 | +# ----- BUTTON ----- |
| 62 | +# Assign text in button and the function to call when clicked |
| 63 | +button_1 = ttk.Button(frame, text='Click', command=set_entry) |
| 64 | +button_1.grid(column=3, row=1, sticky=(W, E)) |
| 65 | + |
| 66 | +# You can disable the button |
| 67 | +button_1['state'] = 'disabled' |
| 68 | +button_1['state'] = 'enable' |
| 69 | + |
| 70 | +# Check if it is disabled (1 = True, 0 = False) |
| 71 | +entry_1_txt.set(button_1.instate(['disabled'])) |
| 72 | + |
| 73 | +# ----- CHECK BUTTON ----- |
| 74 | +chk_but_1_txt = StringVar() |
| 75 | +# Text to display next to it, function to call, StringVar, |
| 76 | +# value assigned when check and not checked |
| 77 | +chk_but_1 = ttk.Checkbutton(frame, text='Feelings', |
| 78 | + command=chk_but_changed, |
| 79 | + variable=chk_but_1_txt, |
| 80 | + onvalue='Happy', offvalue='Sad') |
| 81 | +chk_but_1.grid(column=4, row=1, sticky=(W, E)) |
| 82 | + |
| 83 | +# ----- RADIO BUTTONS ----- |
| 84 | +# Only 1 radio button can be selected at a time |
| 85 | +# Shared StringVar |
| 86 | +radio_but_1_val = StringVar() |
| 87 | +# Parent, text assigned, StringVar, value assigned to StringVar, |
| 88 | +# function to call on event |
| 89 | +red_r_but = ttk.Radiobutton(frame, text='Red', |
| 90 | + variable=radio_but_1_val, |
| 91 | + value='Red', command=radio_changed) |
| 92 | +blue_r_but = ttk.Radiobutton(frame, text='Blue', |
| 93 | + variable=radio_but_1_val, |
| 94 | + value='Blue', command=radio_changed) |
| 95 | +green_r_but = ttk.Radiobutton(frame, text='Green', |
| 96 | + variable=radio_but_1_val, |
| 97 | + value='Green', command=radio_changed) |
| 98 | +red_r_but.grid(column=2, row=2, sticky=(W, E)) |
| 99 | +blue_r_but.grid(column=3, row=2, sticky=(W, E)) |
| 100 | +green_r_but.grid(column=4, row=2, sticky=(W, E)) |
| 101 | + |
| 102 | +# Label for radio buttons |
| 103 | +label_2 = ttk.Label(frame, text='Fav Color') |
| 104 | +label_2.grid(column=1, row=2, sticky=(W, E)) |
| 105 | + |
| 106 | +# ----- COMBOBOX ----- |
| 107 | +# Drop down boxes that contain a list of values |
| 108 | +combo_1_val = StringVar() |
| 109 | +combo_1 = ttk.Combobox(frame, textvariable=combo_1_val) |
| 110 | +label_3 = ttk.Label(frame, text='Size') |
| 111 | +label_3.grid(column=1, row=3, sticky=(W, E)) |
| 112 | + |
| 113 | +# Assign values to the combobox |
| 114 | +combo_1['values'] = ('Small', 'Medium', 'Large') |
| 115 | +combo_1.grid(column=2, row=3, sticky=(W, E)) |
| 116 | + |
| 117 | +# Call a function when combobox is changed |
| 118 | +combo_1.bind('<<ComboboxSelected>>', combo_changed) |
| 119 | + |
| 120 | +# A loop that executes until the application exits |
| 121 | +root.mainloop() |
0 commit comments