TKinter radio buttons in python

July 18th, 2008 by webstersprodigy

This is pretty straightforward.  I tend to structure all my gui programs so far in almost the same way, with the inherited frame class doing all the packing.

#!/usr/bin/env python

from Tkinter import *

class Demo(Frame):
  def __init__(self, parent=None):
    Frame.__init__(self, parent)
    self.pack()
    Label(self, text="Hiya").pack(side=TOP)
    self.var = StringVar()

    #create radio buttons
    Radiobutton(self, text="Error", command = self.onPress,
                                    variable = self.var,
                                    value=1).pack(anchor=NW)

    Radiobutton(self, text="Input", command = self.onPress,
                                    variable = self.var,
                                    value=2).pack(anchor=NW)

    Button(self, text='State', command=self.report).pack(fill=X)

  def onPress(self):
    pick = self.var.get()
    print "you pressed", pick

  def report(self):
    print self.var.get()

if __name__ == '__main__': Demo().mainloop()

Tags: ,

Leave a Reply


No computers were harmed in the 0.255 seconds it took to produce this page.