亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

基于PyQt4和PySide實現輸入對話框效果

發布時間:2020-09-11 20:33:30 來源:腳本之家 閱讀:170 作者:網海水手 欄目:開發技術

今天做了個基于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界面如下:

基于PyQt4和PySide實現輸入對話框效果

返回的結果中我們注意到字典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_())

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

成武县| 三台县| 疏附县| 鹤山市| 大兴区| 龙泉市| 祁门县| 汨罗市| 唐河县| 龙游县| 临沂市| 夏津县| 华容县| 武定县| 吴忠市| 宁国市| 安阳市| 江口县| 蒙自县| 册亨县| 德格县| 凤庆县| 洛浦县| 清苑县| 专栏| 罗甸县| 海南省| 宾阳县| 湖北省| 额济纳旗| 临漳县| 荔波县| 光泽县| 西贡区| 罗田县| 开化县| 万山特区| 海安县| 元朗区| 哈尔滨市| 乐至县|