from tkinter import *
root = Tk()
root.resizable(False, False)
seconds = 0
is_counting = False
def counting():
global seconds
if is_counting:
seconds += 1
number_of_seconds.config(text=str(seconds))
number_of_seconds.after(1000, counting)
else:
pass
def start():
global is_counting
stop_counting.config(state=NORMAL)
number_of_seconds.after(1000, counting)
is_counting = True
def stop():
global is_counting
global seconds
stop_counting.config(state=DISABLED)
seconds = 0
number_of_seconds.config(text=str(seconds))
is_counting = False
number_of_seconds = Label(text=str(seconds))
start_counting = Button(text="Start", command=start)
stop_counting = Button(text="Stop", state=DISABLED, command=stop)
number_of_seconds.grid(column=0, row=0, columnspan=2)
start_counting.grid(column=0, row=1)
stop_counting.grid(column=1, row=1)
root.mainloop()
from tkinter import *
root = Tk()
root.title("Calculator")
root.resizable(False, False)
number_entry = Entry()
addition = False
subtraction = False
def click(number):
number_of_numbers = len(number_entry.get())
number_entry.insert(number_of_numbers, number)
def clear():
number_entry.delete(0, END)
def plus():
global addition
global before_addition
addition = True
before_addition = int(number_entry.get())
number_entry.delete(0, END)
def minus():
global subtraction
global before_subtraction
subtraction = True
before_subtraction = int(number_entry.get())
number_entry.delete(0, END)
def equals():
global before_addition
global before_subtraction
if addition == True:
current_number = before_addition + int(number_entry.get())
number_entry.delete(0, END)
number_entry.insert(0, current_number)
elif subtraction == True:
current_number = before_subtraction - int(number_entry.get())
number_entry.delete(0, END)
number_entry.insert(0, current_number)
number_1 = Button(text="1", padx=20, pady=20, command=lambda: click(1))
number_2 = Button(text="2", padx=20, pady=20, command=lambda: click(2))
number_3 = Button(text="3", padx=20, pady=20, command=lambda: click(3))
number_4 = Button(text="4", padx=20, pady=20, command=lambda: click(4))
number_5 = Button(text="5", padx=20, pady=20, command=lambda: click(5))
number_6 = Button(text="6", padx=20, pady=20, command=lambda: click(6))
number_7 = Button(text="7", padx=20, pady=20, command=lambda: click(7))
number_8 = Button(text="8", padx=20, pady=20, command=lambda: click(8))
number_9 = Button(text="9", padx=20, pady=20, command=lambda: click(9))
number_0 = Button(text="0", padx=20, pady=20, command=lambda: click(0))
plus_button = Button(text="+", padx=20, pady=51, command=plus)
minus_button = Button(text="-", padx=20, pady=51, command=minus)
equals_button = Button(text="=", padx=47, pady=20, command=equals)
clear_button = Button(text="Clear", padx=63, pady=20, command=clear)
number_entry.grid(column=0, row=0, columnspan=4, sticky=W+E)
number_7.grid(column=0, row=1)
number_8.grid(column=1, row=1)
number_9.grid(column=2, row=1)
number_4.grid(column=0, row=2)
number_5.grid(column=1, row=2)
number_6.grid(column=2, row=2)
number_1.grid(column=0, row=3)
number_2.grid(column=1, row=3)
number_3.grid(column=2, row=3)
number_0.grid(column=0, row=4)
equals_button.grid(column=1, row=4, columnspan=2)
plus_button.grid(column=3, row=1, rowspan=2)
minus_button.grid(column=3, row=3, rowspan=2)
clear_button.grid(column=0, row=5, columnspan=3)
root.mainloop()
from tkinter import *
root = Tk()
root.title("To-Do List")
root.resizable(False, False)
list1 = Listbox()
scroll1 = Scrollbar(command=list1.yview)
list1.config(yscrollcommand=scroll1.set)
entry1 = Entry()
def add_to_list():
if len(entry1.get()) == 0:
pass
else:
entered_text = entry1.get()
list1.insert(list1.size(), entered_text)
entry1.delete(0, END)
def done_from_list():
currently_selected = list1.curselection()
list1.delete(currently_selected)
add = Button(text="Add To List!", command=add_to_list)
done = Button(text="Done!", command=done_from_list)
list1.grid(column=0, row=0, rowspan=3)
scroll1.grid(column=1, row=0, rowspan=3, sticky=N+S)
entry1.grid(column=2, row=0)
add.grid(column=2, row=1)
done.grid(column=2, row=2)
root.mainloop()
1. What is the syntactically correct name of the labels widget?
a. Label()
b. label()
c. labels()
d. Labels()
2. Which of the following can be used with the label widget?
a. relief=""
b. image=""
c. bg=""
d. ipadx=""
3. Match the following parameters to their corresponding functions:
i. fill=""
a. .pack() b. .grid()
ii. sticky=""
a. .pack() b. .grid()
iii. rowspan=""
a. .pack() b. .grid()
iv. side=""
a. .pack() b. .grid()
4. Padx in .grid() _____________ as padx in the Label widget.
a. is the same
b. is not the Same
1. a
2. a, b, d
3. i. a
ii. b
iii. b
iv. a
4. b
1. The default state of a button is:
a. ACTIVE
b. NORMAL
c. DISABLED
2. Is this proper syntax?
button1 = Button(text="Subtract", command=subtract_number(21))
a. True
b. False
3. The function add_number() has to be run after 3 seconds, which of the following should be used?
a. .after(add_number(), 3)
b. .after(3000, add_number())
c. .after(3000, add_number)
d. .after(3, add_number)
e. .after(add_number, 3)
4. How many mistakes are in the following line of code?
button2 = Button(text="Divide", state=disabled, command=divide_number, activeforeground=green)
a. 0
b. 4
c. 1
d. 3
e. 2
1. b
2. b
3. c
4. e
1. .select_present() selects what is present in the entry field.
a. True
b. False
2. Is this proper syntax?
entry1 = Entry(selectforeground="white", width=25, xscrollcommand=scrollbar1)
a. True
b. False
3. What is the function of the .select_clear() method?
a. It deselects what is currently selected.
b. It deletes what is currently selected.
c. It selects everything that is not currently selected.
4. Match the following parameters to their corresponding functions:
i. .icursor() a. Scrollbar() b. Entry()
ii. xscrollcommand= a. Scrollbar() b. Entry()
iii. command= a. Scrollbar() b. Entry()
iv. orient= a. Scrollbar() b. Entry()
v. jump= a. Scrollbar() b. Entry()
vi. show= a. Scrollbar() b. Entry()
1. b
2. b
3. a
4. i. b
ii. b
iii. a
iv. a
v. a
vi. b
1. selectbackground="" changes the background color of only selected entries.
a. True
b. False
2. Is this proper syntax?
listbox1.configure(yscrollcommand=scrollbar1.set)
a. True
b. False
3. What is the function of the .see() method?
a. It changes the index of the entry at the given index such that it becomes visible in the widget.
b. It selects the entry at the given index.
c. Scrolls the listbox widget to a point that the entry at the given index is visible.
4. If the method ".yview_moveto(25)" were to be used in a program, would an error occur?
a. Yes
b. No
1. a
2. b
3. c
4. a
1. resolution= is one of the methods to change the resolution of a scale widget.
a. True
b. False
2. .select() selects the given radiobutton.
a. True
b. False
3. Which of the following methods moves the scale widget slider to a specified number:
a. .get()
b. .move_to()
c. .set()
4. Is the following line of code the correct way to create a radiobutton:
radiobutton2 = Button(text="Number 2", variable=radiovariable, value=2, type="radiobutton")
a. Yes
b. No
1. b
2. a
3. c
4. b
1. Which of the following is correct syntax:
a. frame1.frame()
b. frame1 = frame()
c. frame1.Frame()
d. frame1 = Frame()
2. The following widget will be placed in frame1:
button1 = Button(text="Button").frame1()
a. True
b. False
3. Does the window need to be speficied when adding a widget to a specific frame?
a. No
b. Yes
4. How can new windows be created?
1. d
2. b
3. a
4. New windows can be created by having a new variable set to Tk(), for example: new_window = Tk().
1. The following is the correct format for option menus:
OptionMenu(root, variable, "option", "option", ...)
a. True
b. False
2. Which of the following are methods for the spinbox widget:
a. .increment()
b. .insert()
c. .delete()
d. .clear()
3. What type of variable does the 'values=' parameter have to be set to for spinboxes?
4. The .get() method can be used for which of the following widgets:
a. Option menus
b. Spinboxes
c. Both option menus & spinboxes.
1. a
2. b, c
3. A tuple.
4. c
1. The following is the correct format for drawing a square:
square1 = mycanvas.square(x1, y1, x2, y2, x3, y3, x4, y4)
a. True
b. False
2. When substituting coordinates with a variable, which of the following is the correct format?
a. coords = [10, 4, 8, 2]
b. coords = 10, 4, 8, 2
c. coords = (10, 4, 8, 2)
d. coords = ("10", "4", "8", "2")
3. What method can be used to delete a shape from a canvas?
4. Which of the following is the correct way to create a canvas?
a. mycanvas = canvas()
b. mycanvas.Canvas()
c. mycanvas = Canvas()
d. mycanvas.canvas()
1. b
2. b
3. .delete()
4. c
- Introductory window setup.
- How to use labels.
- Positioning widgets.
- How to use buttons.
- Calling functions with buttons.
- Different widget methods.
- Create a stopwatch GUI program.
- Put labels and buttons to practice.
- How to use entry fields.
- Implement and use a scrollbar.
- Customize entry fields.
- How to use listboxes.
- Manage input data.
- Change listbox appearance.
- Create a functional calculator GUI program.
- Put entry fields to practice.
- Utilize radio and check buttons.
- Customize appearance.
- Perform actions with scale widgets.
- Stay organizized using frames.
- Create new windows.
- Adding widgets to frames.
- Create a functional to-do list GUI program.
- Put listboxes to practice.
- Using/interacting with option menus.
- Configuring spinboxes.
- Manipulating spinbox entries.
Note: I will always include some notes of what I went over during the screen recording so that you can reference to them if you forget anything.
Import tkinter by using: from tkinter import *
root = Tk() initialises the window.
root.geometry("WIDTHxLENGTH") can be used to resize the window.
root.resizable(False, False) can be used to restrict users from resizing the window.
root.mainloop() loops the whole program.
Create a Label by using: variable = Label()
• text=""
• fg="" (Font color)
• bg="" (Background/highlight color)
• font="" (e.g. font="Times 26 bold italic underline")
• justify= (LEFT, RIGHT, CENTER)
• padx="" (I forgot to mention, but if you just type in a number, it will signify character spaces, not pixels, you can also use other units such as: c for centimeter, m for millimeter and i for inch).
• pady=""
• relief= (FLAT, RAISED, SUNKEN, GROOVE, RIDGE, SOLID)
• bd="" (Border width)
• image=variable
Call the label/make it show up on screen by using: variable.pack() or variable.grid()
• side="" (LEFT, RIGHT, TOP, BOTTOM)
• fill="" (X, Y)
• column= (Columns start from 0, i.e. the first column is column 0)
• row= (Rows start from 0, i.e. the first row is column 0)
• columnspan= (Number of columns the element/widget spans)
• rowspan= (Number of rows the element/widget spans)
• padx="" (Padding applied outside of the element/widget boundary)
• pady="" (Padding applied outside of the element/widget boundary)
• ipadx="" (Padding applied inside of the element/widget boundary, same as the padx in Label parameters, except it can only be used in .grid())
• ipady="" (Padding applied inside of the element/widget boundary, same as the pady in Label parameters, except it can only be used in .grid())
• sticky= (N, S, E, W, N+S, S+E, N+S+E+W, etc)
Images can be added to labels by using the image parameter and PhotoImage widget, to add an image to a label, you first need to create a variable that stores the image by using: imageVariable = PhotoImage(file="filename.gif"). Then, you can use the parameter (image=imageVariable) in the Label widget to add it.
If you need any help or have any questions, feel free to message me on Discord at RussianFlipFlop#0.
• text="" (The text in the button.)
• activeforeground="" (Color of foreground when button is pressed.)
• activebackground="" (Color of background when button is pressed.)
• state= (NORMAL, DISABLED, ACTIVE)
• command=function (The function that the button runs when pressed.)
• command=lambda: function() (Can be used to pass arguments or parameters to the function.)
Most of the formatting options/parameters that we used for Labels can also be used for Buttons.
• .invoke() ("Clicks" a button even though the button isn't clicked by anyone. It basically just runs the command that the specific button runs.)
• .config() (Can be used to configure/change the parameters in a widget.)
• .grid_configure() (Can be used to configure/change the parameters in the .grid() method.)
• .after(milliseconds, function) (Can be used to run a function after a given amount of time.)
In this class we worked on a simple stopwatch program which uses both buttons and labels as well as the various methods we learned in the previous classes.
• width="" (Changes the width of the entry widget.)
• justify="" (Changes the position where the text is entered. By default it is set to LEFT, but can be changed to CENTER or RIGHT.)
• selectforeground="" (Changes the color of selected text.)
• selectbackground="" (Changes the background color of selected text.)
• show="" (Changes what the characters are shown as. For example, it can be set to "*", which is useful for password entry fields.)
• xscrollcommand= (Is used to attach/link the scrollbar to the entry field.)
• command= (Is used to attach/link the scrollbar to the entry field.)
• .xview (Is used together with the command parameter for attach/link the scrollbar to the entry field. E.g. command=entry_field_1.xview)
• width="" (Changes the width of the scrollbar.)
• jump= (Makes the entry field "jump" when letting go of the scrollbar. By default it is set to 0, but can be set to 1 to enable the "jump" feature.)
• orient= (Changes the orientation of the scrollbar. Can be set to VERTICAL or HORIZONTAL.)
• .delete(INDEX, INDEX) (Deletes the text within the two given indexes, inclusive of the first index.) (.delete(0, END) can be used to delete everything in an entry field.)
• .get() (Gets/retrieves the text that is in the entry field.)
• .insert(INDEX, "text") (Inserts text at a given index in the entry field. Variables can be used.)
• .icursor(INDEX) (Inserts the cursor at a given index.)
• .select_clear() (Clears the selection. It does not delete the selected text, it just de-selects what is selected.)
• .select_present() (Returns a boolean based off of whether anything is selected inside of the entry field.)
• .select_range(INDEX, INDEX) (Selects the text within the two given indexes, inclusive of the first index.)
• .select_to(INDEX) (Selects up until the given index from where the cursor is currently inserted.)
• .set (Is used together with the xscrollcommand parameter to attach/link the scrollbar to the entry field. E.g. xscrollcommand=scrollbar1.set)
Note: I forgot to go over the .curselection() method. I added it to the methods list, so make sure to take note of it!
• highlightcolor="" (Changes the highlight color of the whole listbox widget.)
• highlightthickness= (Changes the highlight thickness of the whole listbox widget.)
• selectbackground="" (Changes the background color of the currently selected entries.)
• selectmode= (Changes the mode of selection. The various modes are: BROWSE, SINGLE, MULTIPLE, EXTENDED.)
• xscrollcommand= (Used to attach a scrollbar to the listbox widget on the x axis.)
• yscrollcommand= (Used to attach a scrollbar to the listbox widget on the y axis.)
• .insert(INDEX, <to be inserted>) (Inserts a new entry at a given index containing the given text.)
• .get(INDEX) (Gets the text present in the entry that is at the given index.)
• .curselection() (Gets the index/es of the currently selected entries. It is usually paired with the .get() method to get the text in entries selected by the user.)
• .delete(INDEX, INDEX) (Deleted the entries present between the given indexes. The first given index is inclusive.)
• .see(INDEX) (Scrolls the listbox widget to a point that the entry at the given index is visible.)
• .yview_moveto(FRACTION) (Scrolls the listbox widget vertically based on the given fraction ranging from 0 to 1. 0 would make it scroll to the very top, while 1 would make it scroll to the very bottom. Any number between 0 and 1 can be used.)
• .yview_scroll(NUMBER, <units>) (Scrolls the listbox widget vertically based off of the given number of units. The units include UNITS (which are basically lines) and PAGES.)
• .xview_moveto() (Same as yview_moveto() except it scrolls horizontally.)
• .xview_scroll() (Same as yview_scroll() except it scrolls horizontally.)
In this class we worked on a calculator program which after being completed, was able to add and subtract numbers.
• variable= (Sets the variable that gets changed when the radiobutton is selected.)
• value= (Changes the value that the variable is set to when the radiobutton is selected.)
• indicatoron= (Removes the "indicator" circle, giving the button a different look.)
• .deselect() (Deselects the given radiobutton.)
• .select() (Selects the given radiobutton.)
• variable= (Sets the variable that gets changed when the radiobutton is selected.)
• onvalue= (Changes the value that the variable is set to when the checkbutton is selected.)
• offvalue= (Changes the value that the variable is set to when the checkbutton is not selected.)
Same as the methods for radiobuttons.
• from_= (Specifies the starting point for the scale widget.)
• to= (Specifies the ending part for the scale widget.)
• resolution= (Changes the the amount that the number gets incremented by.)
• tickinterval= (Creates labels with the multiples of the specified number next to the scale widget.)
• sliderlength= (Changes the length of the slider.)
• troughcolor= (Changes the color of the trough.)
• command= (Specifies the function that gets run every time the slider is moved.)
• .get() (Gets the number that the scale widget is set to.)
• .set() (Moves the slider to the specified number.)
Note: The audio quality is a bit worse because I don't have my microphone with me.
Frames can be used to divide up a window into different sections so that the whole program can be a bit more organized. To create a frame, you first need to create a variable, and have it set to Frame(), for example: frame1 = Frame().
To add a widget to a frame, the first parameter has to be the variable name of the frame, so for example: button1 = Button(frame1, text="Button").
New windows can be created by having a new variable set to Tk(), for example: new_window = Tk().
To have a widget in a certain window, the first parameter has to be the variable name of the window, for example entry1 = Entry(new_window). The same thing has to be done if you want a frame in a certain window.
If you want to add a widget to a frame which is already in another window, you do not need to specify which window it has to be added to. You only need to specify the frame.
In this class we worked on a To-Do List program which allowed us to add certain things that need to get done in the future, as well as mark them done once they are completed.
Format for OptionMenus: OptionMenu(root, variable, "option", "option", ...)
Make sure that the variable is either set to StringVar(), IntVar(), or DoubleVar().
• .get() (Used for "getting" the value that is selected.)
• .set() (Used for setting the value in the option menu.)
• from_= (Used with the "to" parameter to set the range of numbers for the spinbox.)
• to= (Used with the "from_" parameter to set the range of numbers for the spinbox.)
• values=<tuple variable> (Used to assign the values/options for the spinbox. It can't be used with to and from_.)
• increment= (Used to change by how much the value should increment. E.g. by 0.1 or by 3.)
• .delete(INDEX, INDEX) (Used to delete whatever is present in the "entry field" based off of the given indexes.)
• .insert(INDEX, <to insert>) (Used to insert text into the "entry field" based off of the given index.)
• .get() (Used to "get" the value that is currently in the "entry field".)
Canvases can be used to draw or plot various shapes. To create a canvas you have to create a variable and set it to Canvas(), e.g. mycanvas = Canvas().
• Lines: mycanvas.create_line(x1, y1, x2, y2)
• Ovals: mycanvas.create_oval(x1, y1, x2, y2)
• Arcs: mycanvas.create_arc(x1, y1, x2, y2)
• Polygons: mycanvas.create_polygon(x1, y1, x2, y2, ... xn, yn)
• fill="" (Changes the color of the shape.)
I forgot to go over this in the screen recording, so make sure you take note of this:
Instead of writing all of the coordinates in the parentheses, you can create a variable and set it to a list of numbers. (Not a python list, just a normal list.)
E.g: coords = 0, 0, 150, 200
line1 = mycanvas.create_line(coords, fill="blue")
To delete a shape from the canvas, you can utilise the .delete() method. E.g: mycanvas.delete(line1)