Nice Ironpython code Lardmeister, thanks!
If you don't want the frills (color, button) you can really simplify the code using defaults ...

# add '99 bottles of beer' lyrics to a .NET list box
# simplified code using some defaults
# tested with IronPython 2.6 by  vegaseat

import clr

clr.AddReference('System.Windows.Forms')

import System
from System.Windows.Forms import *
from System.Collections import ArrayList

class BeerSong(Form):
    def __init__(self):
        self.Text = '99 Bottles of Beer'
        # create the listbox and fill the form (self) with it
        box = ListBox()
        box.Dock = DockStyle.Fill
        self.Controls.Add(box)

        # move the lyrics into ArrayList then transfer to the box
        array = ArrayList()
        bottle = "%s bottle"
        beer = "s of beer on the wall!"
        take = "Take one down, pass it around,"
        for k in range(99, 0, -1):
            # an exercise in slicing
            s1 = bottle % k + beer[k==1:]
            s2 = (bottle % k + beer[k==1:])[:-13] + "!"
            s3 = bottle % (k-1 or "No")
            s4 = beer[k==2:-1] + "!"
            array.Add(s1)
            array.Add(s2)
            array.Add(take)
            array.Add(s3+s4)
            array.Add(" ")
        box.DataSource = array


Application.Run(BeerSong())

Darn double post again, sorry!

A combobox is a selection widget that displays the current item, and can pop up a list of selectable items. It takes up a minimum amount of screen space.

# pqt_combobox1.py
# explore the PyQT QComboBox widget
# load, select
# ene

from PyQt4.QtCore import *
from PyQt4.QtGui import *

class ComboBox(QWidget):
    def __init__(self, pasta_list, parent=None):
        QWidget.__init__(self, parent)
        # setGeometry(x_pos, y_pos, width, height)
        self.setGeometry(320, 200, 340, 120)
        self.setWindowTitle('Select a pasta from the combo box')

        self.cb = QComboBox(self)
        self.cb.setFocusPolicy(Qt.NoFocus)
        self.cb.move(10, 10)
        # sort the list
        self.pasta_list = sorted(pasta_list)
        # load the combobox
        for pasta in self.pasta_list:
            self.cb.addItem(pasta)
        # bind/connect selection to an action
        self.connect(self.cb, SIGNAL('currentIndexChanged(QString)'),
            self.changedIndex)

    def changedIndex(self, value):
        """item in the combox has been changed/selected"""
        #print("value = %s" % value)  # test
        s = "You selected %s" % value
        self.setWindowTitle(s)


pasta_list = [
'Spaghetti',
'Vermicelli',
'Bucatini',
'Fettuccine',
'Linguine',
'Lasagne',
'Cavatappi',
'Manicotti',
'Macaroni',
'Penne',
'Rigatoni',
'Ziti',
'Farfalle',
'Spatzen',
'Orzo'
]

app = QApplication([])
cobo = ComboBox(pasta_list)
cobo.show()
app.exec_()

It took a while, but I rewrote my pasta combobox program using Ironpython, not too bad if you know a little C#. PyQT is simpler code, but this one adds some color:

# ip_combobox1.py
# ironpython gives access to the Windows .NET or Linux Mono libraries
# download ironpython from:
# http://www.codeplex.com/ironpython
# ComboBox widget load items, select an item
# ene

import clr

clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")

import System
from System.Windows.Forms import *
from System.Drawing import *


class MyForm(System.Windows.Forms.Form):
    def __init__(self, pasta_list):
        self.initializeComponent()
        self.comboBox1.Sorted = True
        for item in pasta_list:
            self.comboBox1.Items.Add(item)
        # show first item
        self.comboBox1.Text = self.comboBox1.Items[0]

    def initializeComponent(self):
        self.comboBox1 = System.Windows.Forms.ComboBox()
        self.SuspendLayout()
        #
        # comboBox1
        #
        self.comboBox1.FormattingEnabled = True
        self.comboBox1.Location = System.Drawing.Point(12, 12)
        self.comboBox1.Name = 'comboBox1'
        self.comboBox1.Size = System.Drawing.Size(150, 26)
        self.comboBox1.TabIndex = 0
        self.comboBox1.SelectedValueChanged += \
            self.comboBox1_SelectedValueChanged
        #
        # Form1
        #
        self.BackColor = System.Drawing.Color.Brown
        self.ClientSize = System.Drawing.Size(323, 121)
        self.Controls.Add(self.comboBox1)
        self.Font = System.Drawing.Font('Courier New', 12.0,
            System.Drawing.FontStyle.Regular,
            System.Drawing.GraphicsUnit.Point, 0)
        self.Name = 'Form1'
        self.Text = 'Select a pasta from the combo box'
        self.ResumeLayout(False)

    def comboBox1_SelectedValueChanged(self, sender, e):
        """display present selction"""
        s = "Selected pasta is %s" % self.comboBox1.Text
        self.Text = s
        pass


pasta_list = [
'Spaghetti',
'Vermicelli',
'Bucatini',
'Fettuccine',
'Linguine',
'Lasagne',
'Cavatappi',
'Manicotti',
'Macaroni',
'Penne',
'Rigatoni',
'Ziti',
'Farfalle',
'Spatzen',
'Orzo'
]
Application.Run(MyForm(pasta_list))

I know it belongs in the wxPython sticky, but for comparison purposes here it is, the pasta combobox in wx code:

# wx_combobox1.py
# test the wxPython wx.ComboBox() widget
# wx.ComboBox(parent, id, value, pos, size, choices, style)
# a combination of a wx.TextCtrl() and a drop down listbox
# ene

import wx

class MyFrame(wx.Frame):
    def __init__(self, parent, pasta_list):
        wx.Frame.__init__(self, parent, id=-1,
            pos=(320, 200), size=(340, 120),
            title='Select a pasta from the combo box')
        panel = wx.Panel(self)
        panel.SetBackgroundColour("brown")

        self.pasta_list = sorted(pasta_list)
        self.combo = wx.ComboBox(panel, id=-1,
            value=self.pasta_list[0],
            pos=(10, 10), size=(150, 100),
            choices=self.pasta_list)
        # bind mouse click in the dropdown list to an action
        self.combo.Bind(wx.EVT_COMBOBOX, self.selection_changed)

    def selection_changed(self, event):
        """selected item has changed"""
        s = "Selected pasta is %s" % self.combo.GetValue()
        self.SetTitle(s)


pasta_list = [
'Spaghetti',
'Vermicelli',
'Bucatini',
'Fettuccine',
'Linguine',
'Lasagne',
'Cavatappi',
'Manicotti',
'Macaroni',
'Penne',
'Rigatoni',
'Ziti',
'Farfalle',
'Spatzen',
'Orzo'
]

app = wx.App(0)
# create a MyFrame instance and show it
MyFrame(None, pasta_list).Show()
app.MainLoop()

Okay, for a rough comparison here is the Tkinter GUI toolkit. Tkinter itself does not have a combo box widget, but the module ttk added since Python 3.1 has one:

# ttk_combobox3.py
# exploring the Tkinter expansion module ttk combobox
# tested with Python 3.1.1 and Tkinter 8.5
# ene

import tkinter as tk
import tkinter.ttk as ttk

def selection_changed(event):
    """a combo box item has been selected, show the item"""
    s = "You selected %s" % combo.get()
    root.title(s)

pasta_list = [
'Spaghetti',
'Vermicelli',
'Bucatini',
'Fettuccine',
'Linguine',
'Lasagne',
'Cavatappi',
'Manicotti',
'Macaroni',
'Penne',
'Rigatoni',
'Ziti',
'Farfalle',
'Spatzen',
'Orzo'
]

root = tk.Tk()
# window geometry is width x height + x_offset + y_offset
root.geometry("340x120+320+200")
root.title('Select a pasta from the combo box')

combo = ttk.Combobox()
# position the combobox
combo.place(x=10, y=10)
# bind selection to an action
combo.bind('<<ComboboxSelected>>', selection_changed)

# sort the pasta list
pasta_list = sorted(pasta_list)
# load the combo box with the pasta list
combo['values'] = pasta_list

# set the initial pasta
combo.set(pasta_list[0])

root.mainloop()

The Designer program that comes with the PyQt installations lets you drag and drop widgets on a form, modify properties and connect them. When done you save your work to a XML (.ui extension) file. You can load this XML file directly into a simple PyQt program and access it this way:

# run_combobox2.py
# a simple loader for .ui XML files generated with QTDesigner
# the XML file "combobox2.ui"
# contains a QWidget Form with a QComboBox on it
# the QComboBox signal is connected to the Form slot
# setWindowTitle(QString) via currentIndexChanged(QString)
# ene

import sys

from PyQt4 import QtGui, uic


app = QtGui.QApplication(sys.argv)
widget = uic.loadUi("combobox2.ui")
widget.show()

combo = widget.comboBox
# load the combobox with some string items
pasta_list = ['Spaghetti', 'Fettuccine', 'Ziti', 'Penne', 'Lasagne']
for pasta in pasta_list:
    combo.addItem(pasta)

app.exec_()

By the way, my XML file generated by the Qt Designer program looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Form</class>
 <widget class="QWidget" name="Form">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>392</width>
    <height>129</height>
   </rect>
  </property>
  <property name="font">
   <font>
    <family>Arial</family>
    <pointsize>12</pointsize>
   </font>
  </property>
  <property name="windowTitle">
   <string>Select a pasta from the combo box</string>
  </property>
  <widget class="QComboBox" name="comboBox">
   <property name="geometry">
    <rect>
     <x>20</x>
     <y>10</y>
     <width>181</width>
     <height>22</height>
    </rect>
   </property>
  </widget>
 </widget>
 <resources/>
 <connections>
  <connection>
   <sender>comboBox</sender>
   <signal>currentIndexChanged(QString)</signal>
   <receiver>Form</receiver>
   <slot>setWindowTitle(QString)</slot>
   <hints>
    <hint type="sourcelabel">
     <x>136</x>
     <y>21</y>
    </hint>
    <hint type="destinationlabel">
     <x>136</x>
     <y>45</y>
    </hint>
   </hints>
  </connection>
 </connections>
</ui>

You can see that the design information is all in there. If you are brave enough, you can carefully edit the XML file (leave the tags alone!) and see how it changes your program.

Editor's note: changed to combobox2.ui upon OP's request

Here's an example of a simple Hello "Dani web" example in Jython.
Using Java's Swing modules.
I notice that it takes up more memory than standard Python programs on run.
See the image attached for the output of the program.

#!/usr/bin/env jython

from pawt import swing
import sys
from java.awt import Color, BorderLayout

def quit(e):
    sys.exit()

top = swing.JFrame("PySwing")
box = swing.JPanel(BorderLayout())
hello = swing.JLabel("Hello DANIWeb!")
quit = swing.JButton("QUIT", actionPerformed=quit,
    background=Color.red, foreground=Color.white)

box.add("North", hello)
box.add("South", quit)
top.contentPane.add(box)
top.pack()
top.visible = 1	# or True for Jython 2.2+

Dragging one image around the canvas with the mouse can be done using the Tkinter GUI toolkit as shown here:

# explore the Tkinter GUI toolkit
# drag one image on the canvas
# tested with Python 3.1.1 and Tkinter 8.5

import tkinter as tk

class MyCanvas(tk.Frame):
    def __init__(self, master, photo):
        tk.Frame.__init__(self, master)
        # use pack layout in the class
        # expand frame to fit as window is resized
        self.pack(expand='yes')
        self.master = master
        self.photo = photo
        self.canvas = tk.Canvas()
        self.canvas.pack(side='top', fill='both', expand='yes')
        # initial image upper left corner ('nw') at x=0 and y=0
        self.img = self.canvas.create_image(0, 0, image=photo, anchor='nw')
        # drag upper left corner of image
        self.canvas.bind("<B1-Motion>", self.move_image)

    def move_image(self, event):
        # delete the old image
        self.canvas.delete(self.img)
        # get the mouse position
        x = event.x
        y = event.y
        # create the new image at position x, y
        self.img = self.canvas.create_image(x, y, image=self.photo,
            anchor='nw')
        self.canvas.update()


root = tk.Tk()
root.title("drag upper left corner of image")
# pick an image file you have in your working directory
# or specify full path (without PIL you have to use .gif files)
image_file = "fun.gif"
photo = tk.PhotoImage(file=image_file)
MyCanvas(root, photo)
root.mainloop()

Should work with Python2 making minor modifications.

The question of embedding image files into programs came up. Tkinter makes this easy.

First create the base64 encoded image string:

# Base64encGIF2.py
# create base64 image string
# (read as binary file 'rb')
# Henri AB

import base64

# pick file you have in working directory or give full path
gif_file = "grape.gif"
# base64.encodestring() gives 76 char/line string
# base64.b64encode() gives all char on one line
mystr = base64.b64encode(open(gif_file, 'rb').read())
# customize to n char/line
# inserted '\n' is ignored by b64decode()
n = 68
b64_str = ""
for ix, c in enumerate(mystr):
    if ix != 0 and ix % n == 0:
        b64_str += '\n'
    b64_str += c

print "grape_gif='''\\\n" + b64_str + "'''"

"""select and copy resulting base64 gif string to your program -->

grape_gif='''\
R0lGODlhIAAgALMAAAAAAAAAgHCAkC6LV76+vvXeswD/ANzc3DLNMubm+v/6zS9PT6Ai
8P8A/////////yH5BAEAAAkALAAAAAAgACAAAAS00MlJq7046803AF3ofAYYfh8GIEvp
oUZcmtOKAO5rLMva0rYVKqX5IEq3XDAZo1GGiOhw5rtJc09cVGo7orYwYtYo3d4+DBxJ
WuSCAQ30+vNTGcxnOIARj3eTYhJDQ3woDGl7foNiKBV7aYeEkHEignKFkk4ciYaImJqb
kZ+PjZUjaJOElKanqJyRrJyZgSKkokOsNYa2q7mcirC5I5FofsK6hcHHgsSgx4a9yzXK
0rrV19gRADs='''

"""

Second, copy the resulting image string to your program, for eample:

# Base64encGIF_button2.py
# test base64 image string with Tkinter to form image button
# Henri AB

try:
    # Python2
    import Tkinter as tk
except ImportError:
    # Python3
    import tkinter as tk

# base64 image string of grape.gif
grape_gif='''\
R0lGODlhIAAgALMAAAAAAAAAgHCAkC6LV76+vvXeswD/ANzc3DLNMubm+v/6zS9PT6Ai
8P8A/////////yH5BAEAAAkALAAAAAAgACAAAAS00MlJq7046803AF3ofAYYfh8GIEvp
oUZcmtOKAO5rLMva0rYVKqX5IEq3XDAZo1GGiOhw5rtJc09cVGo7orYwYtYo3d4+DBxJ
WuSCAQ30+vNTGcxnOIARj3eTYhJDQ3woDGl7foNiKBV7aYeEkHEignKFkk4ciYaImJqb
kZ+PjZUjaJOElKanqJyRrJyZgSKkokOsNYa2q7mcirC5I5FofsK6hcHHgsSgx4a9yzXK
0rrV19gRADs='''

root = tk.Tk()

# 'data=' converts base64 image string to an actual gif image
gif_image = tk.PhotoImage(data=grape_gif)

button = tk.Button(root, image=gif_image)
button.pack(padx=10, pady=10)
# optionally save button image from garbage collection
button.image = gif_image

root.mainloop()

Note that the base64 image string formatted to 68 char per line shows better on DaniWeb.

Tired of pasta? This example shows you how to show an image file picture with IronPython's .NET GUI tools:

# show an image file using IronPython downloaded from:
# http://www.codeplex.com/ironpython
# modified the example from:
# http://www.voidspace.org.uk/ironpython/winforms/part11.shtml
# image PythonHand3.jpg is attached to post
# http://www.daniweb.com/forums/post958342.html#post958342
# tested with IronPython 2.6    by ene

import clr

clr.AddReference('System.Windows.Forms')
clr.AddReference('System.Drawing')

from System.Windows.Forms import (
    Application, DockStyle, Form, PictureBox, PictureBoxSizeMode
)
from System.Drawing import Image, Size

class MyForm(Form):
    def __init__(self):
        Form.__init__(self)
        # adjust the form's client area size to the picture
        self.ClientSize = Size(400, 300)
        # give the form a title
        self.Text = 'Explore the PictureBox()'

        # pick an image file you have in the working directory
        # or give full pathname
        fname = "PythonHand3.jpg"
        image = Image.FromFile(fname)
        pictureBox = PictureBox()
        # this will fit the image to the form
        pictureBox.SizeMode = PictureBoxSizeMode.StretchImage
        pictureBox.Image = image
        # fit the picture box to the frame
        pictureBox.Dock = DockStyle.Fill

        self.Controls.Add(pictureBox)
        self.Show()


form = MyForm()
Application.Run(form)
commented: very nice +11
commented: thanks for the ironpython code +15

This shows you how to create a keypad with Tkinter. The code uses a for-loop to reduce the amount of typing ...

# create a calculator key pad with Tkinter
# tested with Python 3.1.1 and Python 2.5.4
# vegaseat

try:
    # Python2
    import Tkinter as tk
except ImportError:
    # Python3
    import tkinter as tk

# needs Python25 or higher
from functools import partial


def click(btn):
    # test the button command click
    s = "Button %s clicked" % btn
    root.title(s)


root = tk.Tk()
root['bg'] = 'green'

# create a labeled frame for the keypad buttons
# relief='groove' and labelanchor='nw' are default
lf = tk.LabelFrame(root, text=" keypad ", bd=3)
lf.pack(padx=15, pady=10)

# typical calculator button layout
btn_list = [
'7',  '8',  '9',  '*',  'C',
'4',  '5',  '6',  '/',  'M->',
'1',  '2',  '3',  '-',  '->M',
'0',  '.',  '=',  '+',  'neg' ]

# create and position all buttons with a for-loop
# r, c used for row, column grid values
r = 1
c = 0
n = 0
# list(range()) needed for Python3
btn = list(range(len(btn_list)))
for label in btn_list:
    # partial takes care of function and argument
    cmd = partial(click, label)
    # create the button
    btn[n] = tk.Button(lf, text=label, width=5, command=cmd)
    # position the button
    btn[n].grid(row=r, column=c)
    # increment button index
    n += 1
    # update row/column position
    c += 1
    if c > 4:
        c = 0
        r += 1

root.mainloop()

All in all a great start for a calculator program.

A classy example of the Tkinter GUI toolkit. It shows you how to draw a circle on a canvas and move it around using the arrow keys ...

# use a Tkinter canvas to draw a circle, move it with the arrow keys
# vegaseat

try:
    # Python2
    import Tkinter as tk
except ImportError:
    # Python3
    import tkinter as tk

class MyApp(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.title("move circle with arrow keys")
        # create a canvas to draw on
        self.cv = tk.Canvas(self, width=400, height=400, bg='white')
        self.cv.grid()
        # create a square box with upper left corner (x,y)
        self.x = 120
        self.y = 120
        size = 150
        box = (self.x, self.y, self.x + size, self.y + size)
        # create a circle that fits the box
        self.circle = self.cv.create_oval(box, fill='red')
        # bind arrow keys to movement
        self.bind('<Up>', self.move_up)
        self.bind('<Down>', self.move_down)
        self.bind('<Left>', self.move_left)
        self.bind('<Right>', self.move_right)
        
    def move_up(self, event):
        # move_increment is 5 pixels
        y = -5
        # move circle by increments x, y
        self.cv.move(self.circle, 0, y)

    def move_down(self, event):
        y = 5
        self.cv.move(self.circle, 0, y)

    def move_left(self, event):
        x = -5
        self.cv.move(self.circle, x, 0)

    def move_right(self, event):
        x = 5
        self.cv.move(self.circle, x, 0)


app = MyApp()
app.mainloop()

This little code allows you you to use the mouse wheel on Tkinter with either Windows or Linux ...

# explore the mouse wheel with Tkinter

try:
    # Python2
    import Tkinter as tk
except ImportError:
    # Python3
    import tkinter as tk

class MyApp(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.title("mouse wheel action")
        # use width x height + x_offset + y_offset (no spaces!)
        self.geometry("300x120+50+50")
        self['bg'] = 'dark green'
        self.counter = 0
        # Windows
        self.bind("<MouseWheel>", self.mouse_wheel)
        # Linux
        self.bind("<Button-4>", self.mouse_wheel)
        self.bind("<Button-5>", self.mouse_wheel)

        self.label = tk.Label(self, font=('courier', 16, 'bold'), 
            width=10)
        self.label.pack(padx=20, pady=40)

    def mouse_wheel(self, event):
        """respond to Linux or Windows wheel event"""
        if event.num == 5 or event.delta == -120:
            self.counter -= 1
        if event.num == 4 or event.delta == 120:
            self.counter += 1
        self.label['text'] = self.counter

app = MyApp()
app.mainloop()

Just a quick look at the PyQT QListWidget list box ...

# a simple window using PyQT
# with a button and a listbox to load and select
# tested with Python 3.1.1 and PyQT 4.71
# vegaseat

from PyQt4.QtCore import *
from PyQt4.QtGui import *

class MyForm(QWidget):
    def __init__(self, name_list):
        QWidget.__init__(self)
        # setGeometry(x_pos, y_pos, width, height)
        self.setGeometry(100, 150, 300, 220)
        self.setWindowTitle("Load the listbox first")

        # make name_list available for methods
        self.name_list = name_list

        # use a grid layout for the widgets
        grid = QGridLayout()

        btn_load = QPushButton("Load List")
        # bind the button click to a function reference
        # new connect style, needs PyQt 4.5+
        btn_load.clicked.connect(self.on_click)          

        self.listbox = QListWidget()
        # new connect style, needs PyQt 4.5+
        self.listbox.clicked.connect(self.on_select)        

        # addWidget(widget, row, column, rowSpan, columnSpan)
        grid.addWidget(btn_load, 0, 0, 1, 1)
        # listbox spans over 5 rows and 2 columns
        grid.addWidget(self.listbox, 1, 0, 5, 2)
        self.setLayout(grid)

    def on_click(self):
        """the load button has been clicked, load the listbox"""
        self.listbox.addItems(self.name_list)
        self.setWindowTitle("Select a name ...")

    def on_select(self):
        """an item in the listbox has been clicked/selected"""
        #selected_name = self.listbox.selectedItems()[0].text()
        selected_name = self.listbox.currentItem().text()
        self.setWindowTitle(selected_name)


name_list = [
"Erich Meitinger",
"Udo Baus",
"Jens Klein",
"Bjorn Bork",
"Heidrun Lovelace",
"Klaus Abraham",
"Ulla Jorgens",
"Volger Jenkings",
"Helmut Schmidt",
"Freja Larse",
"Larry Orkan",
"Andreas Mauser",
"Harry Heimlich"
]

app =  QApplication([])
form = MyForm(name_list)
form.show()
app.exec_()

I was wondering if there are any GUI toolkits in python that create native-looking windows in Windows 7. I love python as a language, but I've always thought a lot of the GUIs look like they are from Windows 95.

Any suggestions?

Editor's note:
Again, don't clutter up the sticky with questions. Ask question in the regular forum.

Tkinter. It have you noticed how the notepad buttons look better? That's because of Windows 7, not a new GUI.

I was wondering if there are any GUI toolkits in python that create native-looking windows in Windows 7

Wxpython-PyQt dont have the ugly and native-looking that Tkinter has on all windows version.
Tkinter look better on linux,mac.
I postet a wxpython picture here.
http://www.daniweb.com/forums/thread252780.html
Wxpython are a very good gui-toolkit and you have a lot more option than tkinter.

Hello all,

Since I'm new to Daniweb I'll try a carefull little introduction before dumping code in this thread. I'm a senior software developer at ASML, writing code in C and Python. C for the real-time part, Python for testing, calibration and rapid prototyping. In my spare time I'm developing a Python application with a clock, interval and event timer for autistic people.

There's a small code example displaying a fairly normal, analog clock. Rather than going through all posts and the complete history of Daniweb I ask you here if it would be interesting for this thread. When so, I'll post it.

With regards,
Ad van Gerven

Sounds interesting. You should post this as a code snippet rather than bury in in the back of this thread.

Toggle images with the Tkinter GUI toolkit:

# Tkinter toggle images ...

import Tkinter as tk
from PIL import ImageTk

def toggle_image(toggle=[False]):
    if toggle[0]:
        label.config(image=image1)
        toggle[0] = False
    else:
        label.config(image=image2)
        toggle[0] = True
    root.update()


root = tk.Tk()
root.config(bg='yellow')

# create all image objects in __main__ to be persistent
# pick images you have in the working folder or give full path ...
image1 = ImageTk.PhotoImage(file='redbtn.jpg')
image2 = ImageTk.PhotoImage(file='bluebtn.jpg')

label = tk.Label(root, image=image1)
label.grid(row=1, column=1, padx=20)

button = tk.Button(root,text='toggle image', command=toggle_image)
button.grid(row=2, column=1)

root.mainloop()

Simple code to show you how to access the selected choice of a Tkinter Optionmenu() ...

# using Tkinter's Optionmenu() as a combobox
# allows one item to be selected
# use trace() and globalgetvar(name) to show selected choice

try:
    # Python2
    import Tkinter as tk
except ImportError:
    # Python3
    import tkinter as tk

def show_choice(name, index, mode):
    sf = "value is %s" % root.globalgetvar(name)
    root.title(sf)


root = tk.Tk()

choices = ['red', 'green', 'blue', 'yellow','white']
color = tk.StringVar(root)
color.trace('w', show_choice)
# optionally preselect a choice
color.set(choices[2])
color_option = tk.OptionMenu(root, color, *choices)
color_option.pack(padx=70, pady=10)

root.mainloop()

Just an example of drawing shapes with the pygame module ...

# exploring module pygame

import pygame as pg

# pygame uses (r, g, b) color tuples
white = (255, 255, 255)
blue = (0, 0, 255)

# create a 300 x 300 pixel display window
win = pg.display.set_mode((300, 300))
# optional title bar caption
pg.display.set_caption('A Pygame Drawing')
# default background is black, so make it white
win.fill(white)


# --- draw a blue circle on a white background ---
# center coordinates (x, y)
center = (150, 150)
radius = 100
# width of 0 (default) fills the circle
# otherwise it is thickness of outline
# test it by changing width to 1, 2 or 5
width = 0
# draw.circle(Surface, color, pos, radius, width)
pg.draw.circle(win, blue, center, radius, width)


# update the display window to show the drawing
pg.display.flip()

# event loop and exit conditions
# (windows titlebar x click to exit)
while True:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            raise SystemExit

You can use the above code as a general template to draw many different shapes. Just the center of the code needs to be changed ...

# exploring module pygame
# draw a blue rectangle on a white background

import pygame as pg

# pygame uses (r, g, b) color tuples
white = (255, 255, 255)
blue = (0, 0, 255)

# create a 300 x 300 pixel display window
win = pg.display.set_mode((300, 300))
# optional title bar caption
pg.display.set_caption('A Pygame Drawing')
# default background is black, so make it white
win.fill(white)


# draw a blue rectangle
# a rect object is set with (x1, y1, w, h)
# where (x1, y1) are the upper left corner coordinates
# w and h are the width and height of rect
x1 = y1 = 50
w = h = 200
rect = (x1, y1, w, h)
# width of 0 (default) fills the rectangle
# otherwise it is thickness of outline
width = 2
# draw.rect(Surface, color, rect, width)
pg.draw.rect(win, blue, rect, width)


# update the display window to show the drawing
pg.display.flip()

# event loop and exit conditions
# (windows titlebar x click to exit)
while True:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            raise SystemExit

A triangle is best drawn using a polygon and give the three corner (x, y) points of the triangle. Again, we are using the basic pygame tamplate and change out the drawing part ...

# exploring module pygame

import pygame as pg

# pygame uses (r, g, b) color tuples
white = (255, 255, 255)
blue = (0, 0, 255)

# create a 300 x 300 pixel display window
win = pg.display.set_mode((300, 300))
# optional title bar caption
pg.display.set_caption('A Pygame Drawing')
# default background is black, so make it white
win.fill(white)


# --- draw a blue triangle using a polygon ---
# triangle coordinates are (x1, y1), (x2, y2) and (x3, y3)
x1 = y1 = 50
x2 = 220
y2 = 250
x3 = 220
y3 = 50
# create a list of 3 coordinate points
point_list = [(x1, y1), (x2, y2), (x3, y3)]
# width of 0 (default) fills the rectangle
# otherwise it is thickness of outline
width = 1
# draw.polygon(Surface, color, point_list, width)
pg.draw.polygon(win, blue, point_list, width)


# update the display window to show the drawing
pg.display.flip()

# event loop and exit conditions
# (windows titlebar x click to exit)
while True:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            raise SystemExit

This tutorial is really helpful in understanding pythong GUI programming.

I used the PyQT Designer to create a simple XML file (ButtonList1.ui) containing just a QWidget, a QPushButton, and a QListWidget ...

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Form</class>
 <widget class="QWidget" name="Form">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>272</width>
    <height>259</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Button and List</string>
  </property>
  <widget class="QPushButton" name="button1">
   <property name="geometry">
    <rect>
     <x>10</x>
     <y>10</y>
     <width>111</width>
     <height>23</height>
    </rect>
   </property>
   <property name="text">
    <string>Add item to list</string>
   </property>
  </widget>
  <widget class="QListWidget" name="list1">
   <property name="geometry">
    <rect>
     <x>10</x>
     <y>50</y>
     <width>111</width>
     <height>192</height>
    </rect>
   </property>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

I then wrote a simple loader to used the XML file and PyQT ...

# a simple loader for .ui files generated with QTDesigner
# this UI is an XML file that contains
# QWidget
# QPushButton named button1
# QListWidget named list1
# link from button1 to list1 is via a decorator
# however, you have to use method name on_button1_clicked
# tested with PyQT version 4.7.2

from PyQt4 import QtCore, QtGui, uic

class MyWindow(QtGui.QWidget):
    def __init__(self, *args):
        QtGui.QWidget.__init__(self, *args)
        uic.loadUi("ButtonList1.ui", self)
        self.count = 1
  
    @QtCore.pyqtSignature("")
    def on_button1_clicked(self):
        """button1 has been clicked, add item to list1"""
        s = "This is item%s" % self.count
        self.list1.addItem(s)
        self.count += 1


app = QtGui.QApplication([])
widget = MyWindow()
widget.show()
app.exec_()

The button1 click was not linked to list1 in the Designer, the little decorator used seems to figure that out, as long as you use the method name on_button1_clicked

Python31 not only included the Tkinter extension module ttk, but also the extension module tix. This module has plenty of extra features. Here is an example ...

# Tkx_ScrolledWindow1.py
# Tkinter extension module tix comes with version 3.1 of Python
# explore the tix.ScrolledWindow
# vertical and horizontal scroll bars appear as needed (automatic)
# see for instance C:/Python31/Lib/tkinter/tix.py for details

from tkinter import tix

app = tix.Tk()
app.title('exploring tix')

scr_win = tix.ScrolledWindow(app, width=200, height=300)
scr_win.pack(fill='both', expand=1)

sframe = scr_win.window
sframe.config(bg='brown')
s1 = 'Welcome to tkinter.tix, tkinter on steroids!'
s2 = '... Now included with Python31 ...'
for x in range(20):
    tix.Label(sframe, text=s1, bg='yellow').pack()
    tix.Label(sframe, text=s2, bg='white', fg='red').pack()

app.mainloop()

The module easygui is used in education and is a simple set of Tkinter based popup boxes to interact with the user. Here is an example ...

# exploring easygui from:
# http://easygui.sourceforge.net/
# a simple but somewhat homely GUI toolit based on Tkinter
# using mostly popup boxes for user interaction

import easygui as eg

# create a choice box
msg ="What is your favorite flavor?"
title = "Ice Cream Survey"
choices = ["Vanilla", "Chocolate", "Strawberry", "Rocky Road"]
choice = eg.choicebox(msg, title, choices)

# send result to a message box
s = "One order of %s coming up!" % choice
eg.msgbox(msg=s, title='result', ok_button='OK', image=None, root=None)
Member Avatar for 1stDAN

Thank you all very much. I am now really confident in PyQt4 and I'll have a go at it.

Thanks!

The SharpDevelop IDE is generally used for C# development on .Net framework (Windows) computers. Version 3.2 of this IDE can now be used directly with IronPython to create GUI programs in drag and drop fashion similar to the way that Microsoft's Visual languages use. IronPython 2.6.1 pretty well uses syntax compatible with Python 2.6. The GUI portion of the code looks very similar to a C# program. Well, here is an example of code created with the IDE. The IDE uses a solution approach with a program.py and an .resx XML file to compile the code to an .exe file via CLR. If you don't want the .exe file and its entourage, you can add a few extra lines (principally from program.py) to use the IronPython interpreter (ipy.exe) with the modified code. Here is an example of the modified code ...

# ip_sharp_listbox3.py
# Use the SharpDevelop 3.2 IDE from:
# http://www.icsharpcode.net/OpenSource/SD/Download/#SharpDevelop3x
# and IronPython 2.6.1 from:
# http://www.ironpython.org/
# to create a form with a listbox and 3 buttons
# load the listbox with names, sort the items
# clear the listbox items and show a selected item

# add these lines to the code created with SharpDevelop 3.2 IDE
# to run with the IronPython interpreter (ipy.exe)
import clr
clr.AddReference('System.Drawing')
clr.AddReference('System.Windows.Forms')

# SharpDevelop code starts here ...
import System.Drawing
import System.Windows.Forms

from System.Drawing import *
from System.Windows.Forms import *

class MainForm(Form):
	def __init__(self):
		self.InitializeComponent()
	
	def InitializeComponent(self):
		self._listBox1 = System.Windows.Forms.ListBox()
		self._button1 = System.Windows.Forms.Button()
		self._button2 = System.Windows.Forms.Button()
		self._button3 = System.Windows.Forms.Button()
		self._label1 = System.Windows.Forms.Label()
		self.SuspendLayout()
		# 
		# listBox1
		# 
		self._listBox1.BackColor = System.Drawing.Color.FromArgb(255, 255, 192)
		self._listBox1.FormattingEnabled = True
		self._listBox1.Location = System.Drawing.Point(12, 12)
		self._listBox1.Name = "listBox1"
		self._listBox1.Size = System.Drawing.Size(147, 186)
		self._listBox1.TabIndex = 0
		self._listBox1.SelectedIndexChanged += self.ListBox1SelectedIndexChanged
		# 
		# button1
		# 
		self._button1.BackColor = System.Drawing.Color.FromArgb(192, 192, 255)
		self._button1.Location = System.Drawing.Point(166, 13)
		self._button1.Name = "button1"
		self._button1.Size = System.Drawing.Size(114, 23)
		self._button1.TabIndex = 1
		self._button1.Text = "Load Listbox"
		self._button1.UseVisualStyleBackColor = False
		self._button1.MouseClick += self.Button1MouseClick
		# 
		# button2
		# 
		self._button2.BackColor = System.Drawing.Color.FromArgb(255, 192, 255)
		self._button2.Location = System.Drawing.Point(166, 42)
		self._button2.Name = "button2"
		self._button2.Size = System.Drawing.Size(114, 23)
		self._button2.TabIndex = 2
		self._button2.Text = "Sort Listbox"
		self._button2.UseVisualStyleBackColor = False
		self._button2.MouseClick += self.Button2MouseClick
		# 
		# button3
		# 
		self._button3.BackColor = System.Drawing.Color.FromArgb(255, 224, 192)
		self._button3.Location = System.Drawing.Point(166, 72)
		self._button3.Name = "button3"
		self._button3.Size = System.Drawing.Size(114, 23)
		self._button3.TabIndex = 3
		self._button3.Text = "Clear Listbox"
		self._button3.UseVisualStyleBackColor = False
		self._button3.MouseClick += self.Button3MouseClick
		# 
		# label1
		# 
		self._label1.BackColor = System.Drawing.Color.FromArgb(192, 255, 192)
		self._label1.Location = System.Drawing.Point(12, 212)
		self._label1.Name = "label1"
		self._label1.Size = System.Drawing.Size(146, 19)
		self._label1.TabIndex = 4
		self._label1.Text = ""
		# 
		# MainForm
		# 
		self.BackColor = System.Drawing.Color.Green
		self.ClientSize = System.Drawing.Size(292, 266)
		self.Controls.Add(self._label1)
		self.Controls.Add(self._button3)
		self.Controls.Add(self._button2)
		self.Controls.Add(self._button1)
		self.Controls.Add(self._listBox1)
		self.Name = "MainForm"
		self.Text = "Load Listbox first ..."
		self.ResumeLayout(False)


	def Button1MouseClick(self, sender, e):
		"""load the listbox"""
		# start unsorted
		self._listBox1.Sorted = False
		name_list = [
		'Frank',
		'Egon',
		'Heinz',
		'Lisa',
		'Sarah',
		'Olaf',
		'Arty',
		'Monika',
		'Ludwig',
		'Ted',
		'Paula']
		for name in name_list:
			self._listBox1.Items.Add(name)
		pass

	def Button2MouseClick(self, sender, e):
		"""set the listbox to sorted"""
		self._listBox1.Sorted = True
		pass

	def Button3MouseClick(self, sender, e):
		"""clear the listbox items"""
		self._listBox1.Items.Clear()
		pass

	def ListBox1SelectedIndexChanged(self, sender, e):
		"""show a selected listbox item in label"""
		self._label1.Text = self._listBox1.SelectedItems[0]
		pass

# ... SharpDevelop code ends here

# add this final line to the code created with SharpDevelop 3.2 IDE
# so you can run the code with the IronPython interpreter rather 
# than compile it to an .exe file with SharpDevelop
Application.Run(MainForm())

One rather rather poor thing about using this IDE is that it seems to insist in using tabs for indentations. At least it is consistent and you can bring the code up on a normal Python IDE and convert to the customary 4 spaces per tab.

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.