如果选中单选按钮然后它会显示一些消息(在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”
答案 0 :(得分:2)
选择Radiobutton时,将调用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)
答案 1 :(得分:1)
使用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()