-1

当检查单选按钮时,我该如何进行此消息,然后显示一些消息(在Python 3和Tkinter中)?

例如:

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()

并且当检查其中一个按钮时,它会显示一些消息,例如。“已选中 RadioButton1”

4

2 回答 2

2

选择单选按钮时,将调用选项中使用的函数command(如果存在)。记得传递事件处理函数的引用,也就是不带括号的函数名。

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)
于 2013-03-26T15:44:00.743 回答
1

使用 tkinter 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()
于 2013-03-26T16:30:34.460 回答