基于PyQt4和PySide实现输入对话框效果
作者:网海水手 时间:2023-06-11 13:30:58
今天做了个基于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.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
标签:PyQt4,PySide,对话框
0
投稿
猜你喜欢
Python解析命令行读取参数之argparse模块
2021-12-17 22:15:32
mysql启用skip-name-resolve模式时出现Warning的处理办法
2024-01-20 06:44:58
一文了解MySQL事务隔离级别
2024-01-24 11:23:02
Python中import导入不同目录的模块方法详解
2021-04-08 02:37:08
css实现简单圆角效果
2008-11-27 13:11:00
Python3之文件读写操作的实例讲解
2023-07-27 15:57:45
python 随机打乱 图片和对应的标签方法
2023-03-13 10:45:29
61条面向对象设计的经验原则
2008-05-08 13:05:00
python神经网络使用Keras进行模型的保存与读取
2023-04-15 06:53:34
高级消息队列协议AMQP简介
2022-11-01 09:39:07
Python xlrd/xlwt 创建excel文件及常用操作
2021-08-17 04:33:22
微信小程序(十二)text组件详细介绍
2024-04-19 09:43:53
Python处理mysql特殊字符的问题
2024-01-17 01:28:38
基于Python快速处理PDF表格数据
2021-04-11 23:29:54
js实现选项卡效果
2024-05-09 10:12:01
解析mysql二进制日志处理事务与非事务性语句的区别
2024-01-21 23:54:29
平面设计中的文字设计
2009-01-15 12:09:00
Vue实现购物车功能
2024-04-30 10:26:21
Go语言中如何通过方法为类型添加行为
2024-01-31 18:02:10
pycharm不以pytest方式运行,想要切换回普通模式运行的操作
2022-02-05 15:27:10