Tkinter in Python 3 – simple example

Tkinter documentation for Python 3:

https://docs.python.org/3/library/tkinter.html


Simple example of making a button change some text:

from tkinter import *

def change_text():
    L1['text'] = 'This is the new text.'

root = Tk()

L1 = Label(root, text="This is a label.")
L1.pack()

b1 = Button(root, text="Click Here", command=change_text)
b1.pack()

root.mainloop()

Notes:

  • “command” is the function that’s called when button is clicked
  • L1[‘text’] changes the “text” attribute of the L1 label.
  • The pack() function adds an object to your application window. Without this, the object is created in memory, but it won’t be visible on your screen.
  • When using pack(), the objects are added in a vertical column in the order that you pack them.