pyqt复选框样式(基于PyQt4和PySide实现输入对话框效果)
类别:脚本大全 浏览量:2329
时间:2022-01-18 01:19:11 pyqt复选框样式
基于PyQt4和PySide实现输入对话框效果今天做了个基于pyqt4和pyside的输入对话框.已放到pypi中,包名wlab,大家可以使用pip安装:
pip install wlab
在程序输入中,有时会要求同时改变多个参数值,而且类型也不尽相同,
这时下面的模块比较彻底的解决了这个问题.先看个示例吧.
比如我们有三个参数,分别为int,float,和字符串类型:
|
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的新手,可以复制下面的文件:
|
#-*- 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 .modif
|