Tkinter - Canvas

Canvas is a widget that allows you to draw lines and shapes on the screen. It can also be used as a container widget to add other widgets to.

You can keep references to the the objects you draw on the screen and update them at a later time to create animations and other effects.

canvas.py

from tkinter import *

class GUI:
    def __init__(self, root):
        self.root = root

        # minimum size of window (length, width)
        self.root.minsize(350,200)

        # title
        self.root.title("Canvas")

        self.root.columnconfigure(index=0, weight=1)
        self.root.rowconfigure(index=0, weight=1)

        self.canvas = Canvas(self.root)
        self.canvas.grid(row=0, column=0, sticky=N+S+E+W)

        self.line1 = self.canvas.create_line(0, 0, 350, 200, width=6)

        self.line2 = self.canvas.create_line(50, 200, 200, 30, fill="#FF0000", dash=(2, 4), width=5)

        self.rectangle = self.canvas.create_rectangle(300, 25, 350, 75, fill="#0000FF")

        # we can configure an item after it is drawn using itemconfig
        # this changes the rectangle fill colour from blue to green.
        self.canvas.itemconfig(self.rectangle, fill="#00FF00")

        # we can move an item after it is drawn by using coords
        # this moves the rectangle position in canvas.
        self.canvas.coords(self.rectangle, (100, 25, 150, 75))

        # items are drawn witha z-index in the order they are created.
        # this brings an item to the top of the z-index. 
        self.canvas.tag_raise(self.line1)

root = Tk()
my_gui = GUI(root)
root.mainloop()

canvas.py

……

READ MORE

Tkinter - Entry

Entry lets a user enter a single line of text in a single font. The value can be assigned to a Tkinter IntVar or StringVar.

entry.py

from tkinter import *

class GUI:
    def __init__(self, root):
        self.root = root

        # minimum size of window (length, width)
        self.root.minsize(350,200)

        # title
        self.root.title("Entry")

        # define var
        # this will hold the value of our Entry
        self.var = StringVar()

        self.root.columnconfigure(index=0, weight=1)
        self.root.columnconfigure(index=1, weight=1)
        self.root.rowconfigure(index=0, weight=1)

        l = Label(self.root, text="Entry:")
        l.grid(row=0, column=0, sticky=W+E)
        e = Entry(self.root, text="Checkbutton", textvariable=self.var)
        e.grid(row=0, column=1)

root = Tk()
my_gui = GUI(root)
root.mainloop()

entry.py

……

READ MORE

Tkinter - Checkbutton

CheckButtons can display text and a check box that can be selected.

Note: If parameters are required to be passed to the buttons command= method, you need to call it using a lambda function command=lambda: myFunction(var).

checkbutton.py

from tkinter import *

class GUI:
    def __init__(self, root):
        self.root = root

        # minimum size of window (length, width)
        self.root.minsize(350,200)

        # title
        self.root.title("Checkbutton")

        # define var
        self.var = IntVar()

        self.root.columnconfigure(index=0, weight=1)
        self.root.rowconfigure(index=0, weight=1)

        c = Checkbutton(self.root, text="Checkbutton", variable=self.var, command=self.print)
        c.grid(row=0, column=0)

    def print(self):
        print (self.var.get())

root = Tk()
my_gui = GUI(root)
root.mainloop()

checkbutton.py

……

READ MORE

Tkinter - Button

Buttons can display text, bitmap/image or both. When clicked they run a defined method.

Note: If parameters are required to be passed to the buttons command= method, you need to call it using a lambda function command=lambda: myFunction(var).

button.py

from tkinter import *

class GUI:
    def __init__(self, root):
        self.root = root

        # minimum size of window (length, width)
        self.root.minsize(350,200)

        # title
        self.root.title("Button")

        self.root.columnconfigure(index=0, weight=1)
        self.root.rowconfigure(index=0, weight=1)

        b = Button(self.root, text="Click to Quit", command=self.root.destroy)
        b.grid(row=0, column=0)



root = Tk()
my_gui = GUI(root)
root.mainloop()

button.py

……

READ MORE

Tkinter - Widgets

Here are the standard widgets included with Tkinter.

Widget Description
Button Buttons can display text, icon or both. When clicked they run a defined method.
Canvas Canvas is used to draw lines, shapes and text onto the window. It can also be used as a container widget to add other widgets into it.
Checkbutton Checkbutton is a box that you can check on or off. It can change the value of a Tkinter IntVar or StringVar, invoke a method or both.
Entry Entry lets a user enter a single line of text in a single font. The value can be assigned to a Tkinter IntVar or StringVar.
Frame Frame is a container widget that holds other widgets. They are used to group widgets in complex layouts.
Label Label can diplay text or images.
LabelFrame LabelFrame is a frame widget with an identifying label.
Listbox Listbox is a selectable list of options. Options have to be the same font and can't contain images. You can configure it to be able to select one or multiple options.
Menu Menu can either be a context menu (eg, right click) or a command bar at the top of the window. On Mac, this is outside the window at the top of the screen like normal Mac apps.
Menubutton Menubutton was used in earlier versions of Tkinter to create different types of menus. The standard Menu widget should handle all use cases now.
Message Message is similar to label, but can span multiple lines. It can wordwrap or change it's width to maintain aspect ratio. Text has to be all the same font.
OptionMenu Optionmenu is a dropdown box containing a list of options. It's valve can be linked to a Tkinter IntVar or StringVar.
PanedWindow PanedWindow is a container widget that holds other widgets in panes. The panes can then be resized by clicking and draging inbetween the panes on seperator lines.
Radiobutton Radiobutton are widgets that are used to select a one of many options. They can contain text, images or both. They can run a method when clicked. They can be grouped together and each group can reference one Tkinter IntVar or StringVar.
Scale Scale is a slider the user can adjust to select a numerical value. You can control the minumum, maximum and resolution of the scale.
Scrollbar Scrollbar can be applied to a Listbox, Textbox or Canvas. Scrolling can be horizontally, vertically or both.
Spinbox Spinbox is used to easily select a a limited number of ordered values.
Text Text is used to enter or display multiple lines of text. Text can be plain, or formated to include different fonts images and formatting.
Toplevel Toplevel is used to create aditional windows besides the root widget.