WxInput模块则比较彻底的解决了这个问题

基于wxpython的GUI输入对话框2

在程序输入中,有时会要求同时改变多个参数值,而且类型也不尽相同,

这时TextEntryDialog就显得不适用了.WxInput模块则比较彻底的解决了这个问题.

比如我有三个值要用户交互式设置,一个是int数,一个是str,一个是float,先看示例文件:

[python]
from WInput import InputBox
values={'int':1,'String':'This is String','float':3.5}
title='Setting values:'
rvalues=InputBox(title,values)
print(rvalues)
from WInput import InputBox
values={'int':1,'String':'This is String','float':3.5}
title='Setting values:'
rvalues=InputBox(title,values)
print(rvalues)显示GUI如下:

上面的代码的关键是设置字典values的值.

WxInput会自动根据字典values的内容生成输入界面,

而且返回值的类型确保和原始类型一样.

再比如程序中有任意两个参数Method和num要设置,那么如下就可了:


[html]
title='Setting values:'
values={'Method':'LogLog','Value':3.5}
rvalues=InputBox(title,values)
title='Setting values:'
values={'Method':'LogLog','Value':3.5}
rvalues=InputBox(title,values)
生成的界面如下:

WxInput模块的代码如下:

[python]
#-*- coding:utf-8 -*-
#~ #--------------------------------------------------------------------------------
#~ module:wlab
#~ FileName=WInput.py
#~ Funciton:wx的输入对话框
#~ author:吴徐平
#~ Date:2013-04-28
#~ Email:539688300@qq.com
#~ #-------------------------------------------------
import wx
import wx.lib.sized_controls as wxsc
#~ #-------------------------------------------------
#~ #set value for widgets( StaticText and TextCtrl) height
wh=30
#~ #set value for max width times
mwt=8
#~ #set value for wh times
wht=3
#~ #-------------------------------------------------
class InputDialog(wxsc.SizedDialog):
def __init__(self,title='Setting values:',values={'int':1,'String':'This is String','float':3.5}):
'''''
#~ using it as follow:
#~ dialog = InputDialog(title='Setting values:',values={'int':1,'String':'This is String','float':3.5})
#~ just for test:
#~ dialog = InputDialog()
'''
style = wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER
wxsc.SizedDialog.__init__(self,parent=None, id=-1, title=title, style=style)
self.originvalues=values.copy()
self.modifiedvalues=values.copy()
self.pane = self.GetContentsPane()
self.pane.SetSizerType("form")

maxlen1=mwt*max([len(str(key)) for key in values])
if maxlen1<wh*wht:
maxlen1=wh*wht
maxlen2=mwt*max([len(str(values[key])) for key in values])
if maxlen2<wh*wht:
maxlen2=wh*wht
for key in self.modifiedvalues:
keyStr=str(key)
label=keyStr+' :'
StaticText = wx.StaticText(parent=self.pane,id=-1,label=label,style=wx.ALIGN_RIGHT)
StaticText.SetInitialSize((maxlen1,wh))
value=str(self.modifiedvalues[key])
TextCtrl = wx.TextCtrl(parent=self.pane, id=-1,value=value)
TextCtrl.SetInitialSize((maxlen2,wh))
TextCtrl.SetSizerProps(expand=True)
#~set a name for TextCtrl,so later we can use wx.FindWindowByName()
TextCtrl.Name='TC_'+str(keyStr)
#StaticText.Name='ST_'+str(keyStr)

#~ # add dialog buttons
self.SetButtonSizer(self.CreateStdDialogButtonSizer(wx.OK | wx.CANCEL))
self.Fit()
self.Center()
def GetOriginValue(self):
'''''
#~ if the user select wx.ID_CANCEL,then return originvalues
'''
return self.originvalues
def GetValue(self):
'''''
#~ if the user select wx.ID_OK,then return self.modifiedvalues
'''
for key in self.modifiedvalues:
keyStr=str(key)
TextCtrlName='TC_'+str(keyStr)
TextCtrl=self.FindWindowByName(TextCtrlName)
ovk=self.modifiedvalues[key]
if(type(ovk)==int):
self.modifiedvalues[key]=int(TextCtrl.GetValue().strip())
elif(type(ovk)==float):
self.modifiedvalues[key]=float(TextCtrl.GetValue().strip())
else:
self.modifiedvalues[key]=str(TextCtrl.GetValue())
return self.modifiedvalues
#~ #-------------------------------------------------
def InputBox(title='Setting values',values={'int':1,'String':'This is String','float':3.5}):
'''''
#~ >>>values={'int':1,'String':'This is String','float':3.5}
#~ >>>title='Setting values:'
#~ >>>rvalues=InputBox(title,values)
#~ >>>print(rvalues):
'''
app = wx.PySimpleApp()
dialog = InputDialog(title=title,values=values)
if dialog.ShowModal() == wx.ID_OK:
values= dialog.GetValue()
else:
values=dialog.GetOriginValue()
dialog.Destroy()
app.MainLoop()
return values
##~ #测试InputBox
#if __name__ == '__main__':
#values={'int':1,'String':'This is String','float':3.5}
#title='Setting values'
#rvalues=InputBox(title,values=values)
#print(rvalues)
##~ #-------------------------------------------------
class InputPanel(wx.Panel):
def __init__(self,parent,label='Setting values:',values={'int':1,'String':'This is String','float':3.5}):
'''''
#~ >>>ipl = InputPanel(parent,label='Setting values:',values={'int':1,'String':'This is String','float':3.5})
#~>>> rvalues=ipl.GetValue(self)
'''
wx.Panel.__init__(self,parent=parent, id=-1)
self.modifiedvalues=values.copy(2881064151)
box = wx.StaticBox(self, -1, label=label)
sbsizer = wx.StaticBoxSizer(box, wx.VERTICAL)
gridsizer = wx.FlexGridSizer(cols=2, hgap=5, vgap=5)

maxlen1=mwt*max([len(str(key)) for key in values])
if maxlen1<wh*wht:
maxlen1=wh*3
maxlen2=mwt*max([len(str(values[key])) for key in values])
if maxlen2<wh*wht:
maxlen2=wh*wht
for key in self.modifiedvalues:
keyStr=str(key)
label=keyStr+' :'
StaticText = wx.StaticText(parent=self,id=-1,label=label,style=wx.ALIGN_RIGHT)
StaticText.SetInitialSize((maxlen1,wh))
gridsizer.Add(StaticText, 0, wx.EXPAND|wx.ALIGN_CENTER_VERTICAL|wx.LEFT|wx.RIGHT, 3)
value=str(self.modifiedvalues[key])
TextCtrl = wx.TextCtrl(parent=self, id=-1,value=value)
TextCtrl.SetInitialSize((maxlen2,wh))
gridsizer.Add(TextCtrl, 0, wx.EXPAND|wx.ALIGN_CENTER_VERTICAL|wx.LEFT|wx.RIGHT, 3)
#~set a name for TextCtrl,so later we can use wx.FindWindowByName()
TextCtrl.Name='TC_'+str(keyStr)
sbsizer.Add(gridsizer, 1, wx.EXPAND)
gridsizer.Layout()
PanelSizer = wx.BoxSizer(wx.VERTICAL)
PanelSizer.Add(sbsizer, 0, wx.ALL|wx.EXPAND, 5)
self.SetSizer(PanelSizer)
PanelSizer.Layout()
PanelSizer.Fit(self)
def GetValue(self):
'''''
#~ return self.modifiedvalues
'''
for key in self.modifiedvalues:
keyStr=str(key)
TextCtrlName='TC_'+str(keyStr)
TextCtrl=self.FindWindowByName(TextCtrlName)
ovk=self.modifiedvalues[key]
if(type(ovk)==int):
self.modifiedvalues[key]=int(TextCtrl.GetValue().strip())
elif(type(ovk)==float):
self.modifiedvalues[key]=float(TextCtrl.GetValue().strip())
else:
self.modifiedvalues[key]=str(TextCtrl.GetValue())
return self.modifiedvalues

原文地址:https://www.cnblogs.com/cbryge/p/6145168.html