基于wxPython的GUI实现输入对话框(2)
程序员文章站
2022-04-28 17:24:13
接着上一篇基于wxpython的gui输入对话框1,继续学习。
在程序输入中,有时会要求同时改变多个参数值,而且类型也不尽相同,
这时textentrydialog就显...
接着上一篇基于wxpython的gui输入对话框1,继续学习。
在程序输入中,有时会要求同时改变多个参数值,而且类型也不尽相同,
这时textentrydialog就显得不适用了.wxinput模块则比较彻底的解决了这个问题.
比如我有三个值要用户交互式设置,一个是int数,一个是str,一个是float,先看示例文件:
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要设置,那么如下就可了:
title='setting values:' values={'method':'loglog','value':3.5} rvalues=inputbox(title,values)
生成的界面如下:
wxinput模块的代码如下:
#-*- 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() 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 ##~ #------------------------------------------------- class inputframe(wx.frame): def __init__(self,title='inputframe:',label='setting values:',values={'int':1,'string':'this is string','float':3.5},size=(400,200)): ''' #~ >>>iframe = inputframe(title='inputframe:',label='setting values:',values={'int':1,'string':'this is string','float':3.5},size=(400,200)): #~>>> rvalues=iframe.getvalue() ''' wx.frame.__init__(self,parent=none,title = title,size=size) self.modifiedvalues=values.copy() self.ipl = inputpanel(self,label=label,values=values) #~ #创建flexgridsizer self.flexgridsizer=wx.flexgridsizer( rows=9, cols=1, vgap=5,hgap=5) self.flexgridsizer.setflexibledirection(wx.both) self.rightpanel = wx.panel(self,-1) #~ #测试按钮1 self.button1 = wx.button(self.rightpanel,-1,"testbutton",size=(100,40),pos=(10,10)) self.button1.bind(wx.evt_button,self.getvalue) #~ #加入sizer中 self.flexgridsizer.add(self.button1,proportion =0, border = 5,flag = wx.all | wx.expand) self.rightpanel.setsizer(self.flexgridsizer) self.boxsizer=wx.boxsizer(wx.horizontal) self.boxsizer.add(self.ipl,proportion =-10, border = 2,flag = wx.all | wx.expand) self.boxsizer.add(self.rightpanel,proportion =0, border = 2,flag = wx.all | wx.expand) self.setsizer(self.boxsizer) self.center(wx.both) #~ #按钮事件,用于测试 def getvalue(self,event): self.modifiedvalues=self.ipl.getvalue() #~ print(self.modifiedvalues) return self.modifiedvalues #~ #主程序测试 def testinputframe(): app = wx.pysimpleapp() title='inputframe:' label='setting values:' values={'int':234,'string':'this is string','float':3.5} frame =inputframe(title,label,values) frame.show() app.mainloop() return if __name__ == '__main__': app = wx.pysimpleapp() title='inputframe:' label='setting values:' values={'int':234,'string':'this is string','float':3.5} frame =inputframe(title,label,values) frame.show() app.mainloop()
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: vue实现滑动到底部加载更多效果