Reading about Python? Actually practice it. Try PyChallenge free

Python Tutorial

Tk widgets

Tkinter has several widgets including:

  • Label
  • EditText
  • Images
  • Buttons (Discussed before)

In this article we will show how to use some of these Tkinter widgets. Keep in mind there's a slight difference between Tkinter for Python 2.x and 3.x

Related course
Practice Python with interactive exercises

Label To create a label we simply call the Label() class and pack it.  The numbers padx and pady are the horizontal and vertical padding.

from Tkinter import *

root = Tk() root.title('Python Tk Examples @ pythonspot.com') Label(root, text='Python').pack(pady=20,padx=50)

root.mainloop()

EditText (Entry widget) To get user input you can use an Entry widget.

from Tkinter import *

root = Tk() root.title('Python Tk Examples @ pythonspot.com')

var = StringVar() textbox = Entry(root, textvariable=var) textbox.focus_set() textbox.pack(pady=10, padx=10)

root.mainloop()

Result:

tk entry tk entry

Images Tk has a widget to display an image, the PhotoImage.  It is quite easy to load an image:

from Tkinter import *
import os

root = Tk() img = PhotoImage(file="logo2.png") panel = Label(root, image = img) panel.pack(side = "bottom", fill = "both", expand = "yes") root.mainloop()

Result:

python tk image python tk image

GUI editor An overview of Tkinter GUI editors can be found here:  http://wiki.tcl.tk/4056

BackNext