TextCtrl无法与CheckBox一起正常工作

时间:2015-07-24 03:16:12

标签: python checkbox wxpython wxtextctrl

我编写了一个代码,用于使用CheckBoxTextCtrl从用户接收一些输入数据。问题是当我标记复选框并出现textctrl时,它接受接收输入数据,但不会替换为默认值!

import wx

class mainClass(wx.Frame):
   def __init__(self):
       wx.Frame.__init__(self, None, -1, 'Interface', size=(500, 250))
       self.panel = wx.Panel(self)

       self.checkReplaceJob = wx.CheckBox(self.panel, -1, "Replace data", (35, 60), (235, 20))
       self.Bind(wx.EVT_CHECKBOX, self.replaceJob, self.checkReplaceJob)

       self.RunBut = wx.Button(self.panel, -1, "Run", pos=(150, 150))
       self.Bind(wx.EVT_BUTTON, self.RunClick, self.RunBut)
       self.RunBut.SetDefault()
       self.CloseBut = wx.Button(self.panel, -1, "Close", pos=(250, 150))
       self.Bind(wx.EVT_BUTTON, self.CloseClick, self.CloseBut)

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

   def replaceJob(self, event):
       if(self.checkReplaceJob.IsChecked()):
           self.repJobRetName()
           self.btn = wx.Button(self.panel, wx.ID_ANY, "&Help", pos=(345, 82))
           self.Bind(wx.EVT_BUTTON, self.HelpJobName, self.btn)

   def repJobRetName(self):
       self.label = wx.StaticText(self.panel, -1, label = "New name:", pos=(165,87))
       self.entry = wx.TextCtrl(self.panel, -1, value = u"Task-1", pos=(230, 84))
       repJobName = self.entry.GetValue()
       return repJobName

   def HelpJobName(self, event):
       help = 'Write out new name.'
       wx.MessageBox(help, "Help")

   def RunClick(self, event):
       if(self.checkReplaceJob.IsChecked()):
           replaceName = self.repJobRetName()
           wx.MessageBox('The new name is: ' + replaceName, "Info")

#############=======================
if __name__ == "__main__":
   app = wx.App(False)
   mainClass().Show()
   app.MainLoop()

1 个答案:

答案 0 :(得分:0)

让我确保我理解正确。您希望文本控件在创建时具有默认值,但您希望在实际选择控件时该值消失。那是对的吗?如果是这样,那么您只需要向wx.EVT_SET_FOCUS添加绑定,并在窗口小部件获得焦点时执行一些操作。这是一个例子:

import wx

class mainClass(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, 'Interface', size=(500, 250))
        self.panel = wx.Panel(self)

        self.checkReplaceJob = wx.CheckBox(self.panel, -1, "Replace data", (35, 60), (235, 20))
        self.Bind(wx.EVT_CHECKBOX, self.replaceJob, self.checkReplaceJob)

        self.RunBut = wx.Button(self.panel, -1, "Run", pos=(150, 150))
        self.Bind(wx.EVT_BUTTON, self.RunClick, self.RunBut)
        self.RunBut.SetDefault()
        self.CloseBut = wx.Button(self.panel, -1, "Close", pos=(250, 150))
        self.Bind(wx.EVT_BUTTON, self.CloseClick, self.CloseBut)

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

    def replaceJob(self, event):
        if(self.checkReplaceJob.IsChecked()):
            self.repJobRetName()
            self.btn = wx.Button(self.panel, wx.ID_ANY, "&Help", pos=(345, 82))
            self.Bind(wx.EVT_BUTTON, self.HelpJobName, self.btn)

    def repJobRetName(self):
        self.label = wx.StaticText(self.panel, -1, label = "New name:", pos=(165,87))
        self.entry = wx.TextCtrl(self.panel, -1, value = u"Task-1", pos=(230, 84))
        self.entry.Bind(wx.EVT_SET_FOCUS, self.onFocus)
        repJobName = self.entry.GetValue()
        return repJobName

    def onFocus(self, event):
        current_value = self.entry.GetValue()
        if current_value == "Task-1":
            self.entry.SetValue("")

    def HelpJobName(self, event):
        help = 'Write out new name.'
        wx.MessageBox(help, "Help")

    def RunClick(self, event):
        if(self.checkReplaceJob.IsChecked()):
            replaceName = self.repJobRetName()
            wx.MessageBox('The new name is: ' + replaceName, "Info")

#############=======================
if __name__ == "__main__":
    app = wx.App(False)
    mainClass().Show()
    app.MainLoop()