We continue to learn about how to use TkInter to create GUI interfaces with Python. We’ll learn about Tk Variables, Unbind, Styling Widgets, Menu Bars, Keyboard Shortcuts and a whole lot more.
If you missed the 1st TkInter tutorial watch it first. All of the code and a transcript of the video follows the video below to help you learn.
If you like videos like this consider donating a $1 on Patreon.
[googleplusone]
Code & Transcript
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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
from tkinter import * from tkinter import messagebox # ---------- VARIABLES & UNBIND ---------- def get_data(event): # Output the values for the Widgets with get() print("String :", strVar.get()) print("Integer :", intVar.get()) print("Double :", dblVar.get()) print("Boolean :", boolVar.get()) # You can unbind and rebind an event to a function def bind_button(event): if boolVar.get(): getDataButton.unbind("<Button-1>") else: getDataButton.bind("<Button-1>", get_data) root = Tk() # As I showed previously there are TkInter variables # you can use with Widgets to set and get values strVar = StringVar() intVar = IntVar() dblVar = DoubleVar() boolVar = BooleanVar() # Set the default values with set() strVar.set("Enter String") intVar.set("Enter Integer") dblVar.set("Enter Double") boolVar.set(True) # Assign the variable to either textvariable or variable strEntry = Entry(root, textvariable=strVar) strEntry.pack(side=LEFT) intEntry = Entry(root, textvariable=intVar) intEntry.pack(side=LEFT) dblEntry = Entry(root, textvariable=dblVar) dblEntry.pack(side=LEFT) # Depending on if this check button is selected or not # will determine if we can get data on our Widgets theCheckBut = Checkbutton(root, text="Switch", variable=boolVar) theCheckBut.bind("<Button-1>", bind_button) theCheckBut.pack(side=LEFT) # Call the function get_data on click getDataButton = Button(root, text="Get Data") getDataButton.bind("<Button-1>", get_data) getDataButton.pack(side=LEFT) root.mainloop() # ---------- STYLING WIDGETS ---------- # There are many ways to custom style your widgets # You can open message boxes def open_msg_box(): messagebox.showwarning( "Event Triggered", "Button Clicked" ) root = Tk() # You can define the size of the window and the # position on the screen with # widthxheight+xoffset+yoffset root.geometry("400x400+300+300") # You can make it so the window isn't resizable root.resizable(width=False, height=False) frame = Frame(root) # Your can change a styling option like this # Color option names are here http://wiki.tcl.tk/37701 # For the font list the font family, px and font style style = ttk.Style() style.configure("TButton", foreground="midnight blue", font="Times 20 bold italic", padding=20) # Ttk widget names : TButton, TCheckbutton, TCombobox, # TEntry, TFrame, TLabel, TLabelframe, TMenubutton, # TNotebook, TProgressbar, TRadiobutton, TScale, # TScrollbar, TSpinbox, Treeview # You can change the theme style for your applications # This shows you all the themes for your OS print(ttk.Style().theme_names()) # You can see current style settings like this print(style.lookup('TButton', 'font')) print(style.lookup('TButton', 'foreground')) print(style.lookup('TButton', 'padding')) # Change the theme for every widget ttk.Style().theme_use('clam') # Have the button open a message box on click theButton = ttk.Button(frame, text="Important Button", command=open_msg_box) # You can also disable and enable buttons theButton['state'] = 'disabled' theButton['state'] = 'normal' theButton.pack() frame.pack() root.mainloop() # ---------- MENU BARS ---------- # Quits the TkInter app when called def quit_app(): root.quit() # Opens a message box when called def show_about(event=None): messagebox.showwarning( "About", "This Awesome Program was Made in 2016" ) root = Tk() # Create the menu object the_menu = Menu(root) # ----- FILE MENU ----- # Create a pull down menu that can't be removed file_menu = Menu(the_menu, tearoff=0) # Add items to the menu that show when clicked # compound allows you to add an image file_menu.add_command(label="Open") file_menu.add_command(label="Save") # Add a horizontal bar to group similar commands file_menu.add_separator() # Call for the function to execute when clicked file_menu.add_command(label="Quit", command=quit_app) # Add the pull down menu to the menu bar the_menu.add_cascade(label="File", menu=file_menu) # ----- FONT MENU FOR VIEW MENU ----- # Stores font chosen and will update based on menu # selection text_font = StringVar() text_font.set("Times") # Outputs font changes def change_font(event=None): print("Font Picked :", text_font.get()) # Define font drop down that will be attached to view font_menu = Menu(the_menu, tearoff=0) # Define radio button options for fonts font_menu.add_radiobutton(label="Times", variable=text_font, command=change_font) font_menu.add_radiobutton(label="Courier", variable=text_font, command=change_font) font_menu.add_radiobutton(label="Ariel", variable=text_font, command=change_font) # ----- VIEW MENU ----- view_menu = Menu(the_menu, tearoff=0) # Variable changes when line numbers is checked # or unchecked line_numbers = IntVar() line_numbers.set(1) # Bind the checking of the line number option # to variable line_numbers view_menu.add_checkbutton(label="Line Numbers", variable=line_numbers) view_menu.add_cascade(label="Fonts", menu=font_menu) # Add the pull down menu to the menu bar the_menu.add_cascade(label="View", menu=view_menu) # ----- HELP MENU ----- help_menu = Menu(the_menu, tearoff=0) # accelerator is used to show a shortcut # OSX, Windows and Linux use the following options # Command-O, Shift+Ctrl+S, Command-Option-Q with the # modifiers Control, Ctrl, Option, Opt, Alt, Shift, # and Command help_menu.add_command(label="About", accelerator="command-H", command=show_about) the_menu.add_cascade(label="Help", menu=help_menu) # Bind the shortcut to the function root.bind('<Command-A>', show_about) root.bind('<Command-a>', show_about) # Display the menu bar root.config(menu=the_menu) root.mainloop() |
Leave a Reply