I keep reading threads here, some use the Tkinter GUI toolkit and others use the wxPython GUI toolkit. Why would one use one or the other?

Recommended Answers

All 12 Replies

If you just want to tinker with GUI coding to get a feel for it, Tkinter is simpler to code. For real involved GUI development I would use wxPython. It has a large number of fancy widgets for grids, mulimedia (MP3, midi, avi, mpg, wav, au), image display (jpg, png, animated gif etc.), HTML window, and so on.

Here is a real simple graphics module (a thin wrapper for Tkinter) for beginner's GUI projects ...
http://mcsp.wartburg.edu/zelle/python/graphics.py
which also has a nice documentation ...
http://mcsp.wartburg.edu/zelle/python/graphics/graphics.pdf

I have also wanted to know that. But I am a beginner to GUI, and since you say Tkinter is easier, are their any good tutorials with the beginner in mind for Tkinter?

Before I forget, you can also go into the DaniWeb Code Snippets section and search the Python Snippets for Tkinter. There are a fair number of Tkinter code snippets in there at various levels of complexity.

You can also search for wxPython and get a similar response.

The "Starting Python" sticky also has a number of examples of GUI programming. For a beginner, this one shows you how to develop a blah looking console "Hello World!" into a mouse clicking colorful program ...
http://www.daniweb.com/techtalkforums/post304759-96.html

Here is vegaseat's fancy Tkinter GUI "Hello World!" code:

# a simple "Hello, world!" Tkinter GUI
# add some color and a larger text font
# add buttons to change text color and fancy them up

# import Tkinter as namespace tk
import Tkinter as tk 

def white_blue():
    label1.config(fg='white', bg='blue')

def blue_green():
    label1.config(fg='blue', bg='green')

# create the basic window, let's call it 'root'  
root = tk.Tk()
# why not add a title
root.title("Hello World from DaniWeb!")

# create a label with colors in it
font1 = ('times', 36, 'bold')
label1 = tk.Label(root, text="Hello, world!", font=font1, fg='red', bg='yellow') 
# let's use a grid to put the label into the window
# make it span two grid columns
label1.grid(row=0, column=0, columnspan=2)
# create 2 buttons to click for text color change
# use the two grid positions below the label
# (by default the button will take up the center of each grid space)
# give the buttons some y-axis space and a ridge style
button1 = tk.Button(root, text="white on blue", relief='ridge', command=white_blue)
button1.grid(row=1, column=0, pady=5)
button2 = tk.Button(root, text="blue on green", relief='ridge', command=blue_green)
button2.grid(row=1, column=1, pady=5)

# run the GUI event loop
root.mainloop()

Here is the same program written in wxPython, seems to be slightly more complex:

# a simple "Hello, world!" wxPython GUI
# add some color and a larger text font
# add buttons to change text color and fancy them up

import wx

def white_blue(event):
    label1.SetBackgroundColour("blue")
    label1.SetForegroundColour("white")
    label1.SetLabel("Hello World!")

def blue_green(event):
    label1.SetBackgroundColour("green")
    label1.SetForegroundColour("blue")
    label1.SetLabel("Hello World!")

app = wx.PySimpleApp()

# create the basic window, let's call it frame1
# (there is no parent, -1 is the default ID)
# unlike Tkinter this window does not auto-size
# you must change it's size to fit the larger text
frame1 = wx.Frame(None, -1, size=(440, 150))

# add a title to the frame
frame1.SetTitle("Hello World from DaniWeb!")

# create a label, default is auto-size
label1 = wx.StaticText(frame1, -1, "Hello World!")
# add color to the label
label1.SetBackgroundColour("yellow")
label1.SetForegroundColour("red")
# give the label a larger font
label1.SetFont(wx.Font(36, wx.MODERN, wx.NORMAL, wx.BOLD))

# create the 2 buttons
button1 = wx.Button(frame1, -1, "white on blue")
button2 = wx.Button(frame1, -1, "blue on green")
# bind the 2 buttons to a function
button1.Bind(wx.EVT_BUTTON, white_blue)
button2.Bind(wx.EVT_BUTTON, blue_green)

# wx.GridBagSizer() is similar to Tkinters grid()
# the label is in top grid spanning across 2 grids
# the 2 buttons are in adjoining grids below
# vgap=0, hgap=0 and span=(1, 1) are defaults
# pos=(row, column)  span=(rowspan, columnspan)
sizer1 = wx.GridBagSizer(vgap=5, hgap=5)
sizer1.Add(label1, pos=(0, 0), span=(1, 2))
# center buttons in grid space
sizer1.Add(button1, (1, 0), (1, 1), wx.ALIGN_CENTER)
sizer1.Add(button2, (1, 1), (1, 1), wx.ALIGN_CENTER)
frame1.SetSizer(sizer1)

# show the window
frame1.Show(True)

# run the GUI event loop
app.MainLoop()

I looked at Ene's wxPython remake of vegaseat's fancy Tkinter GUI "Hello World!" code, and used the Boa Constructor IDE to do this. It took only a few minutes to create this wxPython code, since Boa has a drag/drop frame builder and writes most of the code:

# fancy "Hello World!" wxPython code generated mostly by Boa Constructor
# Boa Constructor (needs wxPython, has drag/drop frame builder, debugger etc.)
# Further development may have stopped.  Download free from:
# http://freshmeat.net/redir/boa-constructor/832/url_zip/boa-constructor-0.4.4.zip

#Boa:Frame:Frame1

import wx

def create(parent):
    return Frame1(parent)

[wxID_FRAME1, wxID_FRAME1BUTTON1, wxID_FRAME1BUTTON2, wxID_FRAME1STATICTEXT1, 
] = [wx.NewId() for _init_ctrls in range(4)]

class Frame1(wx.Frame):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Frame.__init__(self, id=wxID_FRAME1, name='', parent=prnt,
              pos=wx.Point(396, 174), size=wx.Size(391, 178),
              style=wx.DEFAULT_FRAME_STYLE, title=u'Hello World from DaniWeb!')
        self.SetClientSize(wx.Size(383, 138))

        self.staticText1 = wx.StaticText(id=wxID_FRAME1STATICTEXT1,
              label=u' Hello World! ', name='staticText1', parent=self,
              pos=wx.Point(0, 0), size=wx.Size(385, 84), style=0)
        self.staticText1.SetBackgroundColour(wx.Colour(255, 255, 0))
        self.staticText1.SetForegroundColour(wx.Colour(255, 0, 0))
        self.staticText1.SetFont(wx.Font(36, wx.SWISS, wx.NORMAL, wx.NORMAL,
              False, u'Comic Sans MS'))

        self.button1 = wx.Button(id=wxID_FRAME1BUTTON1, label=u'white on blue',
              name='button1', parent=self, pos=wx.Point(56, 96),
              size=wx.Size(96, 28), style=0)
        self.button1.Bind(wx.EVT_BUTTON, self.OnButton1Button,
              id=wxID_FRAME1BUTTON1)

        self.button2 = wx.Button(id=wxID_FRAME1BUTTON2, label=u'blue on green',
              name='button2', parent=self, pos=wx.Point(216, 96),
              size=wx.Size(103, 28), style=0)
        self.button2.Bind(wx.EVT_BUTTON, self.OnButton2Button,
              id=wxID_FRAME1BUTTON2)

    def __init__(self, parent):
        self._init_ctrls(parent)

    def OnButton1Button(self, event):
        # I had to add these lines, Boa generates the rest
        self.staticText1.SetBackgroundColour("blue")
        self.staticText1.SetForegroundColour("white")
        self.staticText1.SetLabel(" Hello World! ")        
        #event.Skip()

    def OnButton2Button(self, event):
        # I had to add these lines, Boa generates the rest
        self.staticText1.SetBackgroundColour("green")
        self.staticText1.SetForegroundColour("blue")
        self.staticText1.SetLabel(" Hello World! ")        
        #event.Skip()


if __name__ == '__main__':
    app = wx.PySimpleApp()
    wx.InitAllImageHandlers()
    frame = create(None)
    frame.Show()

    app.MainLoop()

Nice to see that folks still use Boa Constructor. It is a very nice IDE that includes a Delphi like frame builder/designer. According to Werner Bruhin development has not stopped, it simply plays hard to get. The latest version I have seen (middle of 2006) was 0.5.2

Another frame builder/designer for wxPython is wxGlade. This one is not an IDE. It takes a little effort to get used to it. For that effort it can spit out Python, Lisp or C++ code.

Here is the fancy "Hello World!" wxPython version created with wxGlade ...

#!/usr/bin/env python
# -*- coding: ISO-8859-1 -*-
# generated by wxGlade 0.4.1 on Tue Jan 23 23:49:34 2007
 
import wx
 
class MyFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        # begin wxGlade: MyFrame.__init__
        kwds["style"] = wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)
        self.sizer_1_staticbox = wx.StaticBox(self, -1, "1")
        self.sizer_2_staticbox = wx.StaticBox(self, -1, "2")
        self.label_1 = wx.StaticText(self, -1, " Hello World! ")
        self.button_1 = wx.Button(self, -1, "white on blue")
        self.button_2 = wx.Button(self, -1, "blue on green")
        self.__set_properties()
        self.__do_layout()
        self.Bind(wx.EVT_BUTTON, self.white_blue, self.button_1)
        self.Bind(wx.EVT_BUTTON, self.blue_green, self.button_2)
        # end wxGlade
 
    def __set_properties(self):
        # begin wxGlade: MyFrame.__set_properties
        self.SetTitle("Hello World from DaniWeb!")
        self.label_1.SetMinSize((-1, -1))
        self.label_1.SetBackgroundColour(wx.Colour(255, 255, 0))
        self.label_1.SetForegroundColour(wx.Colour(255, 0, 0))
        self.label_1.SetFont(wx.Font(36, wx.MODERN, wx.NORMAL, wx.BOLD, 0, ""))
        self.button_1.SetMinSize((-1, -1))
        self.button_2.SetMinSize((-1, -1))
        # end wxGlade
 
    def __do_layout(self):
        # begin wxGlade: MyFrame.__do_layout
        sizer_1 = wx.StaticBoxSizer(self.sizer_1_staticbox, wx.VERTICAL)
        sizer_2 = wx.StaticBoxSizer(self.sizer_2_staticbox, wx.HORIZONTAL)
        sizer_1.Add(self.label_1, 0, wx.ADJUST_MINSIZE, 0)
        sizer_2.Add(self.button_1, 0, wx.ADJUST_MINSIZE, 0)
        sizer_2.Add(self.button_2, 0, wx.ADJUST_MINSIZE, 0)
        sizer_1.Add(sizer_2, 1, wx.EXPAND, 0)
        self.SetAutoLayout(True)
        self.SetSizer(sizer_1)
        sizer_1.Fit(self)
        sizer_1.SetSizeHints(self)
        self.Layout()
        # end wxGlade
    
    def white_blue(self, event): # wxGlade: MyFrame.<event_handler>
        # next 3 lines added by user
        self.label_1.SetBackgroundColour("blue")
        self.label_1.SetForegroundColour("white")
        self.label_1.SetLabel(" Hello World! ")        
        #print "Event handler `white_blue' not implemented!"
        #event.Skip()
    
    def blue_green(self, event): # wxGlade: MyFrame.<event_handler>
        # next 3 lines added by user
        self.label_1.SetBackgroundColour("green")
        self.label_1.SetForegroundColour("blue")
        self.label_1.SetLabel(" Hello World! ") 
        #print "Event handler `blue_green' not implemented!"
        #event.Skip()
 
# end of class MyFrame

class MyApp(wx.App):
    def OnInit(self):
        wx.InitAllImageHandlers()
        frame_1 = MyFrame(None, -1, "")
        self.SetTopWindow(frame_1)
        frame_1.Show()
        return 1
 
# end of class MyApp
 
if __name__ == "__main__":
    app = MyApp(0)
    app.MainLoop()

Is boa constructor easy to use? I'm guessing not, but worth asking.

Boa Constructor has very many features, for beginner takes time to get familiar. I use it, but some things are not intuitive and hard to find in the extensive menu. You can try it, and I can answer some of your questions. I think vegaseat knows more about it.

You can of course also use Boa for your regular console programs too, it has nice editor/IDE. I think Boa is great tool to learn wxPython things.

Try to download the installer exe for Windows from:
http://downloads.sourceforge.net/boa-constructor/boa-constructor-0.4.4.win32.exe

I like Boa because it has a Delphi RAD look and feel to it. The problem with Boa is wxPython, they keep adding so many new and fancy widgets with each release that Boa has a real tough time keeping up. Right now the Boa developers are bogged down with their Unix versions for Linux and the Mac.

There is also a forum for Boa Constructor questions ...
http://sourceforge.net/forum/forum.php?thread_id=1617033&forum_id=5483

The "Starting Python" sticky also has a number of examples of GUI programming. For a beginner, this one shows you how to develop a blah looking console "Hello World!" into a mouse clicking colorful program ...
http://www.daniweb.com/techtalkforums/post304759-96.html

Hey thanks vegas! Your "Hello World" development series is real cool! Like to see a few more of those!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.