在TKinter中的回调函数中取消选择小部件

时间:2018-10-23 09:34:34

标签: python tkinter

我在python2中使用了TKinter。

我的代码如下。

from Tkinter import *  


class Application(Frame):
    def __init__(self, master=None,w=1000,h=600):
        Frame.__init__(self, master)
        self.createWidgets(master,w,h)

    def getRadio(self,widget):
        widget.deselect()


    def createWidgets(self, master,w,h):
        ConfigPane=Frame(master,bg='lightblue',width=int((w/6)*4),height=int(h/3),padx=5,pady=5)
        DisplayPane=Frame(master,bg='DarkOliveGreen1',width=int((w/6)*4),height=int((h/3)*2),padx=5,pady=5)
        HyperPane=Frame(master,bg='khaki1',width=int((w/6)*2),height=h,padx=5,pady=5)
        # layout all of the main containers
        root.grid_rowconfigure(0, weight=1)
        root.grid_rowconfigure(1, weight=1)        
        ConfigPane.grid(row=0,column=0,columnspan=4,rowspan=1, sticky=W+N)
        DisplayPane.grid(row=1,columnspan=4,rowspan=2, sticky=W+S)
        HyperPane.grid(row=0,column=5,columnspan=2,rowspan=3, sticky=E+N+S)
        # create the widgets for the top frame
        var=StringVar()
        RegNet = Radiobutton(ConfigPane, text='RegNet',variable=var,pady=10,padx=10,width=10,anchor='w',command=lambda:self.getRadio(RegNet))
        RegNet.grid(row=0,column=0)           
        InceptionNet = Radiobutton(ConfigPane, text='InceptionNet',variable=var,pady=1,padx=10,width=10,anchor='w',command=lambda:self.getRadio(InceptionNet))
        InceptionNet .grid(row=1,column=0)
        ResNet = Radiobutton(ConfigPane, text='ResNet',variable=var,pady=8,padx=10,width=10,anchor='w',command=lambda:self.getRadio(ResNet))
        ResNet.grid(row=2,column=0)  

if __name__ == "__main__":
    root = Tk()
    width = root.winfo_screenwidth()
    height = root.winfo_screenheight()
    root.geometry(str(width)+'x'+str(height))
    app = Application(master=root,w=width,h=height)
    app.master.title('Deep Learning Reconfigurable Platform')
    app.mainloop()
    root.destroy()

当我单击单选按钮时,按钮上的黑点应该消失了,但是没有消失。我该如何运作?

2 个答案:

答案 0 :(得分:0)

每个单选按钮都需要一个独特的value,否则它们都具有相同的值并因此显示相同的状态。该值是tkinter知道应在一组按钮中选择哪个按钮的方式。

您可能不应该在回调中调用deselect,这会使您的按钮实际上无用,因为用户选择按钮的任何尝试都会导致其单击的内容立即被取消选择。他们将无法选择任何东西。

答案 1 :(得分:0)

关键要素:

  • “ var”必须在Class的范围内定义,如果没有,则在退出createWidget方法时会被销毁,并且您需要在各个小部件之间导航以获取所选的小部件。
  • RadioButtons缺少value属性,定义了被选中时要设置为“ var”的值。

这是更新的代码;已针对Python3更新(更改了模块tkinter名称)

from tkinter import *


class Application(Frame):
    var = None
    def __init__(self, master=None,w=1000,h=600):
        Frame.__init__(self, master)
        self.var = IntVar()
        self.var.set(1)
        self.createWidgets(master, w, h)

    def show_choice(self):
        #for widget in widgets:
         #   widget.deselect()
        print(self.var.get())
        return


    def createWidgets(self, master,w,h):
        ConfigPane=Frame(master,bg='lightblue',width=int((w/6)*4),height=int(h/3),padx=5,pady=5)
        DisplayPane=Frame(master,bg='DarkOliveGreen1',width=int((w/6)*4),height=int((h/3)*2),padx=5,pady=5)
        HyperPane=Frame(master,bg='khaki1',width=int((w/6)*2),height=h,padx=5,pady=5)
        # layout all of the main containers
        root.grid_rowconfigure(0, weight=1)
        root.grid_rowconfigure(1, weight=1)
        ConfigPane.grid(row=0,column=0,columnspan=4,rowspan=1, sticky=W+N)
        DisplayPane.grid(row=1,columnspan=4,rowspan=2, sticky=W+S)
        HyperPane.grid(row=0,column=5,columnspan=2,rowspan=3, sticky=E+N+S)
        # create the widgets for the top frame
        RegNet = Radiobutton(ConfigPane, text='RegNet',variable=self.var,pady=10,padx=10,width=10,anchor='w', value=1, command=self.show_choice)
        RegNet.grid(row=0,column=0)
        InceptionNet = Radiobutton(ConfigPane, text='InceptionNet',variable=self.var,pady=1,padx=10,width=10,anchor='w', value=2, command=self.show_choice)
        InceptionNet.grid(row=1,column=0)
        ResNet = Radiobutton(ConfigPane, text='ResNet',variable=self.var,pady=8,padx=10,width=10,anchor='w', value=3, command=self.show_choice)
        ResNet.grid(row=2,column=0)



if __name__ == "__main__":
    root = Tk()
    width = root.winfo_screenwidth()
    height = root.winfo_screenheight()
    root.geometry(str(width)+'x'+str(height))
    app = Application(master=root,w=width,h=height)
    app.master.title('Deep Learning Reconfigurable Platform')
    app.mainloop()
    # root.destroy()