单选按钮如何记住我的选择

单选按钮如何记住我的选择

本文介绍了wxPython:当我关闭框架时,单选按钮如何记住我的选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有一个主框架和一个按钮,按下后会打开第二个框架.第二帧有 6 个单选按钮.我的问题是,当我选择与已经选择的单选按钮不同的单选按钮并关闭框架时,当我再次打开它(不关闭整个程序)时,为什么选择第一个按钮,如何保留新选择?

Hello I have one main frame and a button which when its pressed a second frame opens. The second frame have 6 radio buttons. My question is when I choose a radiobutton different from the allready selected and close the frame and when I open it again (without close the entire program) why is selected the first one and how can I keep my new selection?

这是秒帧代码的一部分:

Here is some part of the seconds frame code:

    self.radio1 = wx.RadioButton(self, label="1 sec",pos=(35,35),)
    self.Bind(wx.EVT_RADIOBUTTON, self.SetLab1, id=self.radio1.GetId())

    self.radio2 = wx.RadioButton(self, label="2 sec",pos=(35,55))
    self.Bind(wx.EVT_RADIOBUTTON, self.SetLab2, id=self.radio2.GetId())

    self.radio3 = wx.RadioButton(self, label="4 sec",pos=(35,75))
    self.Bind(wx.EVT_RADIOBUTTON, self.SetLab3, id=self.radio3.GetId())
                                  .
                                  .
                                  .

    self.button0=AB.AquaButton(self,label="Exit",pos=(115,142),size=(90,35))
    self.Bind(wx.EVT_BUTTON, self.OnButton0, self.button0)

def OnButton0(self, event):
    self.Close()

def SetLab1(self,event):
    global Delay
    Delay = 'A2/'

def SetLab2(self,event):
    global Delay
    Delay = 'A3/'

def SetLab3(self,event):
    global Delay
    Delay = 'A4/'

推荐答案

如果你关闭框架,你就会破坏它,当你再次构建它时,它会回到默认状态.

if you close the frame you destroy it and when you build it again it will go back to its default state.

您可以执行以下操作:

self.radio1 = wx.RadioButton(self, label="1 sec",pos=(35,35),)
self.Bind(wx.EVT_RADIOBUTTON, self.SetLab1, id=self.radio1.GetId())

self.radio2 = wx.RadioButton(self, label="2 sec",pos=(35,55))
self.Bind(wx.EVT_RADIOBUTTON, self.SetLab2, id=self.radio2.GetId())

self.radio3 = wx.RadioButton(self, label="4 sec",pos=(35,75))
self.Bind(wx.EVT_RADIOBUTTON, self.SetLab3, id=self.radio3.GetId())
global Delay
if Delay is not None:
     getattr(self,"radio"+str(int(Delay[1])-1)).SetValue(True)

这将选择与全局 Delay 变量中的值匹配的单选按钮.

that will select the radio button that matches the value in the global Delay variable.

更简单的解决方案不是关闭"框架而是隐藏它

An easier solution is not to "Close" the frame but rather to hide it

#instead of my_frame.Close() (or my_frame.Destroy())
my_frame.Hide()

这会保留构造的框架,因此当您下次显示它时,它仍将具有其所有值

this keeps the constructed frame so when you next show it it will still have all its values

这篇关于wxPython:当我关闭框架时,单选按钮如何记住我的选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 14:21