Tk window and button


Tk button with onClick event
To create a Tkinter window with a button use the example below.  The program enters mainloop()  which wait for events (user actions). We define the button which has a callback to the function callback().   master is the root window, the window where your button will appear in.

from Tkinter import *
 
master = Tk()
 
def callback():
    print "click!"
 
b = Button(master, text="OK", command=callback)
b.pack()
 
mainloop()
tk button
tk button

Tk image button
If you want an image button, use the PhotoImage class. We set the size of the window and the miminum size with the functions minsize() and geometry(). Example:

from Tkinter import *
 
master = Tk()
master.minsize(300,100)
master.geometry("320x100")
 
def callback():
    print "click!"
 
 
photo=PhotoImage(file="add.png")
b = Button(master,image=photo, command=callback, height=50, width=150)
b.pack()
 
mainloop()

Result:

tk image button
tk image button

Tk Image button with text label
If you want both an image and text, simply add the parameter compound=LEFT.

from Tkinter import *
 
master = Tk()
master.minsize(300,100)
master.geometry("320x100")
 
def callback():
    print "click!"
 
 
photo=PhotoImage(file="add.png")
b = Button(master,image=photo, text="OK", command=callback, height=50, width=150, compound=LEFT)
b.pack()
 
mainloop()

Result:

tk button with text and image
tk button with text and image

Button location
If you want to place the button on your coordinates do not use the pack() function but instead use the function place(x,y), as shown in the example below:

from Tkinter import *
 
master = Tk()
master.minsize(300,100)
master.geometry("320x100")
 
def callback():
    print "click!"
 
 
photo=PhotoImage(file="add.png")
b = Button(master,image=photo, text="OK", command=callback, height=50, width=150, compound=LEFT)
b.place(x = 20, y = 20)
 
mainloop()

Result:

tk button location
tk button location

5 thoughts on “Tk window and button

  1. Paul - October 6, 2015

    when i import the wx and Tkinter, neither is working. I get an import error message and it says that there is no module named wx or Tkinter. Do i have to download these modules or what should i do? Am using python 3.4.3 on win 8.1. Thanks.

    1. Frank - October 7, 2015

      Hi, you should install either of these two modules. You could use pip or easyinstall for that.
      If you use a development editor such as pycharm you can do that from the editor, otherwise:
      See: http://www.wxpython.org/download.php

  2. Fabi.Choi - May 18, 2015

    PhotoImage function doesn’t open ‘png’ files.
    It can be open ‘GIF, PGM and PPM’.

    follow this article : http://stackoverflow.com/questions/27599311/tkinter-photoimage-doesnt-not-support-png-image

    1. Frank - May 18, 2015

      Thanks for the comment! This interesting. PhotoImage function opens PNG under Linux. Traditional formats are GIF, PGM and PPM, perhaps the Linux version of the Tk package is custom or you have an old Tkinter version. Do you have the latest Tkinter version?

      The developers of Tk mention: “Built-in PNG Image Support: Photo images now support read/write in the PNG format, with the ability to set the alpha channel.” http://www.tcl.tk/software/tcltk/8.6.html

      I’ll have a look when I’m on a windows machine.

      1. Paasl - August 25, 2015

        import ImageTk

        photo=ImageTk.PhotoImage(file=”add.png”)