tkinter button
Python hosting: Host, run, and code Python in the cloud!
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 * |

Related course
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 * |
Result:

Tk Image button with text label
If you want both an image and text, simply add the parameter compound=LEFT.
from Tkinter import * |
Result:

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 * |
Result:

If you are new to programming Tkinter, I highly recommend this course.
Leave a Reply:
PhotoImage function doesn't open 'png' files.
It can be open 'GIF, PGM and PPM'.
follow this article : http://stackoverflow.com/qu...
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.
import ImageTk
...
photo=ImageTk.PhotoImage(file="add.png")
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.
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