基于wxPython的GUI实现输入对话框(2)

时间:2021-10-11 02:30:55

接着上一篇基于wxpython的gui输入对话框1,继续学习。

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

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

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

?
1
2
3
4
5
from winput import inputbox
values={'int':1,'string':'this is string','float':3.5}
title='setting values:'
rvalues=inputbox(title,values)
print(rvalues)

显示gui如下:

基于wxPython的GUI实现输入对话框(2)

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

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

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

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

?
1
2
3
title='setting values:'
values={'method':'loglog','value':3.5}
rvalues=inputbox(title,values)

生成的界面如下:

基于wxPython的GUI实现输入对话框(2)

wxinput模块的代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#-*- 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()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/rumswell/article/details/8867203