我正在研究一个收集和检查用户输入的简单程序。除了在用户提供的输入未通过检查时显示消息框 之外,我还想在输入字段的右侧添加提示 ,告诉用户需要什么类型的数据。要做到这一点,我已经创建了一个单行FlexGridSizer包括以下内容:wxPython:使用Show()和Hide()
[(wx.StaticText, “姓名”),(wx.TextCtrl, “在这里用户输入”),(wx.StaticText ,“Input Guidance”)]
初始化时,“Input Guidance”小部件被隐藏。如果用户没有在提供的字段中输入任何内容,然后点击确定按钮,我想三件事情发生
- 场改变颜色,以提醒用户一个问题
- 的“输入向导”窗口小部件(该上述排在第三位)变为可见
- 该对话框会自动调整大小拿现在可见的部件
到目前为止,我只能得到(1)工作,并正在寻找有帮助的帐户(2 )和(3)。
import wx
class Not_Empty(wx.PyValidator):
def __init__(self):
wx.PyValidator.__init__(self)
def Clone(self):
return Not_Empty()
################################################################################
def Validate(self, win):
""""""
evt_location = self.GetWindow()
val = evt_location.GetValue()
if val == "":
evt_location.SetBackgroundColour(wx.Color(250,200,230))
MyDialog().nameWarning_ST.Show(True)
return False
else:
return True
################################################################################
def TransferToWindow(self):
return True
def TransferFromWindow(self):
return True
class MyDialog(wx.Dialog):
def __init__(self):
wx.Dialog.__init__(self, parent=None, id=-1, title="Getting Input", style=wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER)
# Create field labels
name = wx.StaticText(self, -1, "Name")
# Create user-input widgets
name_TC = wx.TextCtrl(self, validator=Not_Empty())
# Create and hide warning boxes
self.nameWarning_ST = wx.StaticText(self, label="Field cannot be left empty", name="emptyAlert")
# to see what I want the dialog to look like AFTER the user has entered an
# empty string change 'False' to 'True' in the line below.
self.nameWarning_ST.Show(False)
# Create accept/cancel buttons
btns = self.CreateButtonSizer(flags=wx.OK|wx.CANCEL)
self.mainSizer = wx.BoxSizer(wx.VERTICAL)
fgs = wx.FlexGridSizer(cols = 3, rows = 1)
fgs.AddMany([(name, -1, wx.ALL, 5), (name_TC, -1, wx.ALL, 5), (self.nameWarning_ST, -1, wx.ALL, 5)])
self.mainSizer.AddMany([(fgs, 1, wx.ALL, 0), (btns, 1, wx.ALL|wx.EXPAND, 5)])
self.SetSizer(self.mainSizer)
self.mainSizer.Fit(self)
if __name__ == '__main__':
app = wx.App()
dlg = MyDialog()
dlg.Center()
dlg.ShowModal()
dlg.Destroy()
app.MainLoop()
得到Mike回应的代码有点儿是狗的早餐。为了使我的目标更清晰,尽管这确实意味着Mike的代码与新改进的问题无关 –