我正在研究一個收集和檢查用戶輸入的簡單程序。除了在用戶提供的輸入未通過檢查時顯示消息框 之外,我還想在輸入字段的右側添加提示 ,告訴用戶需要什麼類型的數據。要做到這一點,我已經創建了一個單行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的代碼與新改進的問題無關 –