Graphical Widgets

The components that make up apps

TkInter Widgets

The Tkinter library provides a selection of widgets
we can use to build our apps.

Window

Open awesomeness.py and create a window for your app:

import tkinter
window = tkinter.Tk()
window.mainloop()

You should see a window when you run your app.

Custom Window

Customise your app window with additional options:

import tkinter
window = tkinter.Tk()

window.title("App of Awesomeness")
window.geometry("300x300")

window.mainloop()

Your window should now be bigger and have a title

Label

Create a label and add it to your window:

window.title("App of Awesomeness")
window.geometry("300x300")

label = tkinter.Label(window)
label.config(text="Hello")
label.grid()

window.mainloop()

Your window should now contain a label “Hello”

Button

Create a button and add it to your window:

label = tkinter.Label(window)
label.config(text="Hello")
label.grid()

button = tkinter.Button(window)
button.config(text="Click Me!")
button.grid()

window.mainloop()

Your window should now contain a button

Entry

Add a text entry box to your window:

button = tkinter.Button(window)
button.config(text="Click Me!")
button.grid()

entry = tkinter.Entry(window)
entry.grid()

window.mainloop()

Your window should now contain a text entry area

Listbox

Add a list box to your app:

entry = tkinter.Entry(window)
entry.grid()

listbox = tkinter.Listbox(window)
listbox.grid()

window.mainloop()

Your window should now contain a list box

Challenge:
Application Form

Cat App Example

Recreate this app using what you have learned.

Thumbs Up!

Graphical Widgets: Complete!

Great, now let’s do some more complex layouts…

Take me to the next chapter!

Loading...

App Window

Basic Example

window = tkinter.Tk()
window.mainloop()

Complex Example

window = tkinter.Tk()

window.title("App of Awesomeness")
window.geometry(300, 300)

window.mainloop()

Properties

None of note

Functions

  • mainloop
  • title
  • geometry

Label

Basic Example

my_label = tkinter.Label(app_window)
my_label.config(text="Hello!")
my_label.grid()

Complex Example

my_label = tkinter.Label(app_window)
my_label.config(text="Hello!", fg="red", font="Comic Sans MS 16 bold")
my_label.grid()

Properties

  • text
    The words to display on the label.
  • fg
    The text colour. The letters “fg” stand for “foreground”.
  • font
    Font properties including the actual font, size in pixels, and whether it should be bold or italic.

Functions

None of note

Entry

Basic Example


Complex Example


Properties

None of note

Functions

None of note

Text

Basic Example


Complex Example


Properties

None of note

Functions

None of note

Button

Basic Example


Complex Example


Properties

None of note

Functions

None of note

Listbox

Basic Example


Complex Example


Properties

None of note

Functions

None of note