今天做了个基于pyqt4和pyside的输入对话框.已放到pypi中,包名wlab,大家可以使用pip安装:
pip install wlab
在程序输入中,有时会要求同时改变多个参数值,而且类型也不尽相同,
这时下面的模块比较彻底的解决了这个问题.先看个示例吧.
比如我们有三个参数,分别为int,float,和字符串类型:
1
2
3
4
5
|
values = { 'string' : 'this is string' , 'float' : 3.5 , 'int' : 15 } groupboxtitle = 'please set values:' title = 'qinputbox:' rvalues = qinputbox(values = values,groupboxtitle = groupboxtitle,title = title) print (rvalues) |
生成的gui界面如下:
返回的结果中我们注意到字典rvalues里面数据的类型和字典valuse中时一致的.
也就是说:
如果values中是整数,那么rvalue中的值也是整数;
如果value中是浮点数,那么rvalue中的值也是浮点数;
如果value中是字符串,那么rvalue中的值也是字符串;
目前只支持这三种类型.但对values中输入参数并没有没有数目限制.
对于不会使用pip的新手,可以复制下面的文件:
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
|
#-*- coding:utf-8 -*- #~ #-------------------------------------------------------------------------------- #~ module:wlab #~ filename=wpyqtinput.py #~class:qinputgroupbox,qinputdialog,qinputbox #~ author:wu xuping #~ date:2013-05-06 #~ email:539688300@qq.com #~ remark:based on pyqt4 or pyside #~ #------------------------------------------------- try : from pyqt4 import qtgui from pyqt4 import qtcore from pyqt4.qtcore import pyqtslot from pyqt4.qtcore import pyqtsignal ispyqt = true ispyside = false except importerror: from pyside import qtgui from pyside import qtcore from pyside.qtcore import slot as pyqtslot from pyside.qtcore import signal as pyqtsignal ispyqt = false ispyside = true #~ #------------------------------------------------- #~ #------------------------------------------------- #~ #将字符串一特定的长度输出 def formatstr(maxstrlength,s): if ( len (s)<maxstrlength): for n in range (maxstrlength - len (s)): s = ' ' + s rs = str (s) + ':' return rs class intlineedit(qtgui.qlineedit): def __init__ ( self ,num = 0 ): ''' #~ intlineedit(num) ''' qtgui.qlineedit.__init__ ( self ) self .num = num self .settext( str ( self .num)) @pyqtslot ( int ) def setvalue( self ,n): self .settext( str (n)) return ( self .num,n) class floatlineedit(qtgui.qlineedit): def __init__ ( self ,num = 0.0 ): ''' #~ floatlineedit(num) ''' qtgui.qlineedit.__init__ ( self ) self .num = num self .settext( str ( self .num)) @pyqtslot ( int ) def setvalue( self ,n): if ( self .num> 0 ): self .settext( str ( self .num + n * self .num / 50.0 )) else : self .settext( str ( self .num - n * self .num / 50.0 )) return ( self .num,n) #~ #------------------------------------------------- class qinputgroupbox(qtgui.qgroupbox): def __init__( self , values = { 'string' : 'this is string' , 'float' : 3.5 , 'int' : 15 },title = 'please set values' ,ntimes = 2.0 ,parent = none): ''' #~#--------------------------------------------------- #~#examples: #~#--------------------------------------------------- #~ >>>values={'string':'this is string','float':3.5,'int':15} #~ >>>groupboxtitle='please set values' #~ >>>self.qigbox=qinputgroupbox(values=values,title=groupboxtitle,ntimes=2.0,parent=self) #~ >>>rvalues=self.qigbox.getoriginvalue() #~ >>>rvalues=self.qigbox.getmodifiedvalues() #~#--------------------------------------------------- #~#parameters: #~#--------------------------------------------------- #~#values={'string':'this is string','float':3.5,'int':15} #~#title='please set values' #~#ntimes=2.0 #~#parent=none ''' qtgui.qgroupbox.__init__ ( self , title = title,parent = parent) self .originvalues = values.copy() self .modifiedvalues = values.copy() maxstrlength = max ([ len ( str (s)) for s in list (values.keys())]) layout = qtgui.qgridlayout() cnt = 0 for key in self .modifiedvalues: label = formatstr(maxstrlength, str (key)) keylabel = qtgui.qlabel(label) layout.addwidget(keylabel, cnt, 0 ) ovk = self .modifiedvalues[key] if ( type (ovk) = = int ) : valuelineedit = intlineedit(ovk) layout.addwidget(valuelineedit, cnt , 1 ) slider = qtgui.qslider(orientation = qtcore.qt.horizontal) if (ovk> 0 ): slider.setrange( ovk / (ntimes + 1 ), ovk * (ntimes + 1 )) elif (ovk = = 0 ): slider.setrange( - 5 * (ntimes + 1 ), 5 * (ntimes + 1 )) else : slider.setrange(ovk * (ntimes + 1 ),ovk / (ntimes + 1 )) slider.setvalue(ovk) qtcore.qobject.connect(slider,qtcore.signal( 'valuechanged(int)' ), valuelineedit, qtcore.slot( 'setvalue(int)' )) layout.addwidget(slider, cnt , 2 ) elif ( type (ovk) = = float ) : valuelineedit = floatlineedit(ovk) layout.addwidget(valuelineedit, cnt , 1 ) slider = qtgui.qslider(orientation = qtcore.qt.horizontal) slider.setrange( - 50 * ntimes, 50 * ntimes) qtcore.qobject.connect(slider,qtcore.signal( 'valuechanged(int)' ), valuelineedit, qtcore.slot( 'setvalue(int)' )) layout.addwidget(slider, cnt , 2 ) else : valuelineedit = qtgui.qlineedit(ovk) layout.addwidget(valuelineedit, cnt, 1 , 1 , 2 ) #~ #set an object name for qlineedit,later we can use findchild() to find it valuelineedit.setobjectname( 'vle' + str (cnt)) layout.setrowstretch(cnt, 5 ) cnt = cnt + 1 layout.setcolumnstretch( 0 , 1 ) layout.setcolumnstretch( 1 , 5 ) layout.setcolumnstretch( 2 , 10 ) self .setlayout(layout) def getoriginvalue( self ): ''' #~ if the user click btn_cancel,then return originvalues ''' return self .originvalues def getmodifiedvalues( self ): ''' #~ if the user click btn_ok,then return self.modifiedvalues ''' cnt = 0 for key in self .modifiedvalues: keystr = str (key) vleobjectname = 'vle' + str (cnt) if (ispyqt): vle = self .findchild((qtgui.qlineedit, ),vleobjectname) else : vle = self .findchild(qtgui.qlineedit,vleobjectname) cnt = cnt + 1 ovk = self .modifiedvalues[key] if ( type (ovk) = = int ): self .modifiedvalues[key] = int (vle.text()) elif ( type (ovk) = = float ): self .modifiedvalues[key] = float (vle.text()) else : self .modifiedvalues[key] = str (vle.text()) return self .modifiedvalues #~ #------------------------------------------------- class qinputdialog(qtgui.qdialog): def __init__( self , values = { 'string' : 'this is string' , 'float' : 3.5 , 'int' : 15 },groupboxtitle = 'please set values' ,title = 'qinputdialog:' ,parent = none): ''' #~ >>>values={'string':'this is string','float':3.5,'int':15} #~ >>>groupboxtitle='please set values' #~ >>>title='qinputdialog:' #~ >>>dlg = qinputdialog(values=values,groupboxtitle=groupboxtitle,title=title,parent=none) #~ >>>if ( dlg.exec_() == qtgui.qdialog.accepted): #~ >>> rvalues = dlg.getmodifiedvalues() #~ >>>else: #~ >>> rvalues = dlg.getoriginvalue() ''' qtgui.qdialog.__init__( self , parent = parent) self .setwindowtitle(title) self .qigbox = qinputgroupbox(values = values,title = groupboxtitle,parent = self ) self .vbox = qtgui.qvboxlayout() self .vbox.addwidget( self .qigbox) self .btn_ok = qtgui.qdialogbuttonbox(qtgui.qdialogbuttonbox.ok) self .btn_cancel = qtgui.qdialogbuttonbox(qtgui.qdialogbuttonbox.cancel) self .btn_ok.clicked.connect( self .accept ) self .btn_cancel.clicked.connect( self .reject) hbox = qtgui.qhboxlayout() hbox.addwidget( self .btn_ok) hbox.addwidget( self .btn_cancel) self .vbox.addlayout(hbox) self .setlayout( self .vbox) def getoriginvalue( self ): ''' #~ if the user click btn_cancel,then return originvalues ''' return self .qigbox.getoriginvalue() def getmodifiedvalues( self ): ''' #~ if the user click btn_ok,then return self.modifiedvalues ''' return self .qigbox.getmodifiedvalues() #~ #------------------------------------------------- #~ #good packaging qinputbox def qinputbox(values = { 'string' : 'this is string' , 'float' : 3.5 , 'int' : 15 },groupboxtitle = 'please set values' ,title = 'qinputbox' ): ''' #~ >>>values={'string':'this is string','float':3.5,'int':15} #~ >>>groupboxtitle='please set values' #~ >>>title='qinputbox' #~ >>>rvalues=qinputbox(values=values,groupboxtitle=groupboxtitle,title=title) #~ >>>print(rvalues) #~ #>>>rvalues=qinputbox(values,groupboxtitle) #~ #>>>rvalues=qinputbox(values) #~ #>>>rvalues=qinputbox() ''' #app = qtgui.qapplication(sys.argv)#已自动创建,无需再创建 dlg = qinputdialog(values = values,groupboxtitle = groupboxtitle,title = title) if ( dlg.exec_() = = qtgui.qdialog.accepted): rvalues = dlg.getmodifiedvalues() else : rvalues = dlg.getoriginvalue() return rvalues #~ #--------------------------------------------------------------------------------------------------------- #~ # main test program for __wpyqtinput__.py #~ #--------------------------------------------------------------------------------------------------------- if __name__ = = '__main__' : #---------------------------------------------------------------------------------------- try : from pyqt4 import qtgui from pyqt4 import qtcore from pyqt4.qtcore import pyqtslot from pyqt4.qtcore import pyqtsignal ispyqt = true ispyside = false except importerror: from pyside import qtgui from pyside import qtcore from pyside.qtcore import slot as pyqtslot from pyside.qtcore import signal as pyqtsignal #---------------------------------------------------------------------------------------- import sys app = qtgui.qapplication(sys.argv) #创建qt进程app #---------------------------------------------------------------------------------------- #示例1 values = { 'string' : 'this is string' , 'float' : - 3.5 , 'int' : - 15 } groupboxtitle = 'please set values' title = 'the first example of qinputbox ' rvalues = qinputbox(values = values,groupboxtitle = groupboxtitle,title = title) print (rvalues) #>>>{'int': -15, 'float': -3.5, 'string': 'this is string'} #---------------------------------------------------------------------------------------- #示例2 from collections import ordereddict values = ordereddict([( 'c' , 1 ), ( 2 , 2 ), ( 'a' , 3 )]) rvalues1 = qinputbox(values = values) print (rvalues1) #>>>ordereddict([('c', 1), (2, 2), ('a', 3)]) #---------------------------------------------------------------------------------------- #示例3 values = { 'string' : 'this is string' , 'float' : - 3.5 , 'int' : - 15 } groupboxtitle = 'please set values' title = 'qinputbox' rvalues2 = qinputbox(values = values,groupboxtitle = groupboxtitle,title = title) print (rvalues2) #>>>{'int': -15, 'float': -3.5, 'string': 'this is string'} #---------------------------------------------------------------------------------------- sys.exit(app.exec_()) |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/rumswell/article/details/8878322