[Tutor] Calling Classes From Tkinter Radio Buttons

Nathan sdragon1984 at gmail.com
Mon Apr 9 00:26:10 CEST 2012


I'm using Tkinter for a GUI for a program of mine, and I'm trying to
use radio buttons to select different functions, but it's not working
right at all. Originally, I had something like

class ClassOne(self):

    def ___str___(self):
        return "This is the base class."

class ClassTwo(ClassOne):

    def __str__(self):
        return "This is the derived class."

in, say, classes.py, and something like

import classes
from Tkinter import *

class Application(Frame):

    def __init__(self, master):
        Frame.__init__(self, master)
        self.grid()
        self.widgets()

    LabelText = classes.ClassOne()

    def widgets(self):
        ClassLabel = Label(self, text = self.LabelText)
        ClassLabel.grid()

root = Tk()
app = Application(root)
root.mainloop()

in, say, GUI.py. This works perfectly fine, but it only sets the label
as "This is the base class." What I'd like is a way to use radio
buttons to configure the text of ClassLabel so that the user can
choose between the two classes.

Currently, I'm trying changing GUI.py to something like:

import classes
from Tkinter import *

class Application(Frame):

    def __init__(self, master):
        Frame.__init__(self, master)
        self.grid()
        self.widgets()

    LabelText = classes.ClassOne()
    classes = ["ClassOne", "ClassTwo"]

    def widgets(self):
        self.variable = IntVar()
        ClassLabel = Label(self, text = self.LabelText)
        ClassLabel.grid(row = 0)
        ClassOneButton = Radiobutton(self, text = "This selects the
base class.", variable = self.variable, value = 0, command =
self.ChangeClass)
        ClassOneButton.grid(row = 1, column = 0)
        ClassTwoButton = Radiobutton(self, text = "This selects the
derived class.", variable = self.variable, value = 1, command =
self.ChangeClass)
        ResetButton = Button(self, text = "Reset the label", command =
self.ResetLabel)

    def ResetLabel(self):
        self.ClassLabel.configure(text = LabelText)

    def ChangeClass(self):
        self.selection = self.variable.get()  # This should get the
value of the selected radio button, right?
        self.LabelText = self.classes[self.selection]() # LabelText
changes according to the radio button.

root = Tk()
app = Application(root)
root.mainloop()


... but when I run this, BOTH radio buttons are selected, and it still
just gives me the base class. What am I doing wrong? Is there a way to
first call a class with a radio button, and then reconfigure the label
with that class? Any help that can be offered is appreciated!


More information about the Tutor mailing list