在我的tkinter应用程序中,我有一个带单选按钮的顶级窗口。当我选择任一单选按钮时,它既不会返回任何值,也不会显示任何错误。
一个最小的示例如下:
import tkinter
from tkinter import *
from tkinter import ttk
class eventReg:
def __init__(self, master):
self.master = master
self.drawBoard()
def drawBoard(self):
self.addButton = ttk.Button(self.master, text="Add New Registration", command=self.newRegistration)
self.addButton.grid(row=1,column=1, sticky='w')
def newRegistration(self):
self.new_registration = tkinter.Toplevel(self.master)
self.new_registration.geometry('700x450+300+200')
self.radioVar = StringVar(master=self.new_registration)
self.nr_radio1 = ttk.Radiobutton(self.new_registration, text="Provide Unique Code", value="provide unicode", variable = self.radioVar)
self.nr_radio2 = ttk.Radiobutton(self.new_registration, text="Scan QR Code", value="scancode", variable=self.radioVar)
self.nr_radio1.grid(row=2, column=1, padx=15, pady=15, sticky='n')
self.nr_radio2.grid(row=2, column=2, padx=15, pady=15, sticky='n')
if self.radioVar.get() == "provide unicode":
self.test_Label = ttk.Label(self.new_registration, text="Working")
self.test_Label.grid(row=2, column=4, padx=15, pady=15)
if __name__ == '__main__':
master=Tk()
eventReg(master)
master.title('Event Registration Manager')
master.mainloop()
答案 0 :(得分:2)
您需要做的就是将回调绑定到您的eventReg
类中的单选按钮。
class eventReg:
...
def newRegistration(self):
...
self.radioVar = StringVar(master=self.new_registration)
self.nr_radio1 = ttk.Radiobutton(self.new_registration, text="Provide Unique Code", value="provide unicode", variable = self.radioVar,command=self.get_var_value)
self.nr_radio2 = ttk.Radiobutton(self.new_registration, text="Scan QR Code", value="scancode", variable=self.radioVar,command=self.get_var_value)
...
def get_var_value(self):
if self.radioVar.get() == "provide unicode":
self.test_Label = ttk.Label(self.new_registration, text="Working")
self.test_Label.grid(row=2, column=4, padx=15, pady=15)