Skip to content

Instantly share code, notes, and snippets.

@massifaqiri
Created July 25, 2020 22:54
Show Gist options
  • Save massifaqiri/d64cffae544e6bc111a4c930f1881fa4 to your computer and use it in GitHub Desktop.
Save massifaqiri/d64cffae544e6bc111a4c930f1881fa4 to your computer and use it in GitHub Desktop.
"""
Learning TkInter
"""
#Import!
from tkinter import *
from tkinter.ttk import *
from tkinter import scrolledtext
from tkinter import messagebox
#Create the window
window = tkinter.Tk()
#Set the size of the window: width & height
window.geometry("300x400")
#Set it as non-resizeable
window.resizeable(0, 0)
#Rename the title of the window
window.title("My Test GUI")
#Create a label saying Hello World, with font Cambria, size 60 and bold AND set its color to red
label = tkinter.Label(window, text = "Hello World!", font=("Arial Bold", 50), fg="red")
label.pack()
label.grid(column=0, row=0)
#Event click here:
def clicked():
label.configure(text="The button was clicked!")
#Create a button that will say Enter AND position it at 3rd column and 2nd row AND set its background to yellow and foreground to blue.
btn = tkinter.Button(window, text="Enter", bg="yellow", fg="blue", command=clicked)
#What is grid used for?
#In order to place the widgets relatively to each other using grids, as following.
btn.grid(column=2, row=0)
#What is a binding function? Functions that are called upon events like, click, etc.
#Some powers of binding functions: Change the current attribute of a widget, as up there AND/OR Creating a new widget, which works as usual
#Create an event that when the button is clicked, the label will say it's clicked
#The code is upstairs!
#Create a textbox before the button, asking for user input:
txt = tkinter.Entry(window, width=10)
txt.grid(column=1, row=0)
#How to get the user's input text: txt.get()
#Create a Combobox with couple of options:
combo = Combobox(window)
combo['values'] = (1,2,3,4,5,"None")
#Choose the selected one to be the 3rd one and put it after the button
combo.current(2)
combo.grid(column=3, row=0)
#Create a checkbutton with default as Checked (at the second row and first column):
chk_state = tkinter.BooleanVar()
chk_state.set(True)
chk = tkinter.Checkbutton(window, text='Are you okay?', var=chk_state)
chk.grid(column=0, row=1)
#Create three radiobuttons at the second row:
rad1 = Radiobutton(window, text='Python', value=1)
rad2 = Radiobutton(window, text='Java', value=2)
rad3 = Radiobutton(window, text='Scala', value=3)
rad1.grid(column=1, row=1)
rad2.grid(column=2, row=1)
rad3.grid(column=3, row=1)
#Create a scrolledtext widget. Note: you should import it first.
scrolledtxt = scrolledtext.ScrolledText(window, width=40, height=10)
scrolledtxt.grid(column=0, row=3)
#Create a dialog box or message box with some content. Note: you should import its library first:
#You can associate it with click
messagebox.showinfo('Message title', 'Message content')
#Create a spinbox:
spin = Spinbox(window, from_=0, to=100, width=5)
spin.grid(column=0, row=2)
'''Geometry Options:
1. Pack: Packs them together. Following options are available:
To fill it in width/height, as parents: w.pack(fill='x') or w.pack(fill='y')
Padding options: Externally(x: padx=#, y: pady=#) & Internally (x: ipadx=#, y: ipady=#)
Placing widgets side by side: padx=#, pady=#, side=tk.LEFT
Pack to a specific side like, left/bottom, etc: pack(side = 'left')
2. Grid: used up! Another feature of grid is columnspan/rowspan which is used for spanning few rows/cols together.
3. Place: Place at absolute/relative positions
label.place(x = #, y = #, width=#, height=#)
What is frame?
It's used to arrange layout in a window, by creating division in the window.
'''
#Create a frame:
top_frame = tkinter.Frame(window).pack()
bottom_frame = tkinter.frame(window).pack(side = 'bottom')
#How to place the widgets on the frame: Simply, in the first parameter of any widget, write a frame instead of window
'''
What does bind do? And what is the syntax?
Bind connects a function to an event. Syntax: btn.bind("<Button-1>", say_hi_func) :- where btn is a widget and say_hi_func is a func with a parameter of event/any other name
What are syntax for the following events:
Left click Button-1
Middle click Button-2
Right click Button-3
Left/Right button released ButtonRelease-1 Or 3
Left/Right double click Double-Button-1 Or 3
The mouse pointer entered the widget (like, mouseover) Enter
The mouse pointer left the widget Leave
Keyboard focus was moved to the widget FocusIn
Keyboard focus was moved out of the widget FocusOut
User pressed any of these keys: Enter, Backspace, Tab, Shift, Control, Alt, Capslock, escape, delete, num lock, scroll lock, f1-f12:
Return, BackSpace, Tab, Shift_L, Control_L, Alt_L, Caps_Lock, Escape, Delete, Num_Lock, Scroll_Lock, F1-F12
Any alphanumeric keyboard key: Key
User changed the size/location of the window: Configure
'''
#Do an image:
icon = tkinter.PhotoImage(file='C:/Users/....')
label = tkinter.Label(window, image = icon)
label.pack()
#Event loop, i.e. it will be open as long as it's not closed like a window.
window.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment