-1

How can I make that when radio button is checked that then it shows some message(in Python 3 and Tkinter)?

for example:

from Tkinter import *

master = Tk()

v = IntVar()

Radiobutton(master, text="One", variable=v, value=1).pack(anchor=W)
Radiobutton(master, text="Two", variable=v, value=2).pack(anchor=W)

mainloop()

and when one of buttons is checked that then it shows some message, for ex. "RadioButton1 is checked"

1
  • Please give more information and code exemple to see the context.
    – fe_lix_
    Mar 26, 2013 at 14:42

2 Answers 2

2

When a Radiobutton is selected, the function used in the command option is called (if present). Remember to pass a reference to the event handler function, that is, the name of the function without parentheses.

def handler():
    print "RadioButton%s is checked" % (v.get())

Radiobutton(master, text="One", variable=v, value=1, command=handler).pack(anchor=W)
Radiobutton(master, text="Two", variable=v, value=2, command=handler).pack(anchor=W)
0
1

Using the command option for tkinters Checkbutton...

from tkinter import *

class Application(Frame):
    def say_hi(self):
        if self.var.get() or self.var2.get():
            msg = ''
            if self.var.get():
                msg = 'Radio button 1 is checked\n'
            if self.var2.get():
                msg += 'Radio button 2 is checked'

            print(msg) # to sdtout...
            top = Toplevel()
            top.title("Pop-up...")
            msg = Message(top, text=msg)
            msg.pack()
            button = Button(top, text="Close", command=top.destroy)
            button.pack()

    def createWidgets(self):
        self.QUIT = Button(self)
        self.QUIT["text"] = "Close"
        self.QUIT["fg"]   = "red"
        self.QUIT["command"] =  self.quit
        self.QUIT.pack({"side": "left"})

        self.var = IntVar()
        self.checkbox = Checkbutton(self)
        self.checkbox["text"] = 'radio1'
        self.checkbox["command"] = self.say_hi
        self.checkbox["variable"] = self.var
        self.checkbox.pack({"side": "left"})

        self.var2 = IntVar()
        self.checkbox2 = Checkbutton(self)
        self.checkbox2["text"] = 'radio2'
        self.checkbox2["command"] = self.say_hi
        self.checkbox2["variable"] = self.var2
        self.checkbox2.pack({"side": "left"})

    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.pack()
        self.createWidgets()

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.