python 单选框radiobutton

2022-05-13 10:50:01 浏览数 (1)

"create a group of radio buttons that launch dialog demos"

from tkinter import * # get base widget set from dialogTable import demos # button callback handlers from quitter import Quitter # attach a quit object to "me"

class Demo(Frame): def init(self, parent=None, **options): Frame.init(self, parent, **options) self.pack() Label(self, text="Radio demos").pack(side=TOP) self.var = StringVar() for key in demos: Radiobutton(self, text=key, command=self.onPress, variable=self.var, value=key).pack(anchor=NW) self.var.set(key) # select last to start Button(self, text='State', command=self.report).pack(fill=X) Quitter(self).pack(fill=X)

代码语言:javascript复制
def onPress(self):
    pick = self.var.get()
    print('you pressed', pick)
    print('result:', demos[pick]())

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

if name == 'main': Demo().mainloop()

0 人点赞