我写了一个代码来接收来自使用CheckBox
和TextCtrl
的用户的一些输入数据。问题是,当我标记复选框和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()
让我确保我理解正确。您希望文本控件在创建时具有默认值,但希望在实际选择该控件时该值消失。对吗?如果是这样,那么您只需要向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()