Pyqt 5 기본 인터페이스 구성 요소 의 inputDialog 사용

12892 단어 Pyqt5inputDialog
QInputDialog 류 는 사용자 의 단일 입력 정 보 를 얻 기 위해 간단 한 대화 상 자 를 제공 합 니 다.문자열,Int 형식 데이터,double 형식 데이터 또는 드 롭 다운 목록 상자 의 항목 일 수 있 습 니 다.
대응 하 는 Dialog 에는 알림 탭,입력 컨트롤(문자열 입력 상 자 를 호출 하면 QLine Edit,Int 형식 이나 double 형식 을 호출 하면 QSpinBox,목록 항목 입력 상 자 를 호출 하면 QComboBox),입력 확인(Ok)단추 와 입력 취소(Cancel)단추 가 포함 되 어 있 습 니 다.
QInputDialog:

class QInputDialog(QDialog)
 | QInputDialog(QWidget parent=None, Qt.WindowFlags flags=0)
QInputDialog 역시 QDialog 에서 계승 하여 간단 한 입력 대화 상 자 를 제공 합 니 다.
코드 예제:
예제 코드 는 다음 과 같다.

#-*- coding:utf-8 -*-
'''
inputDialog
'''
__author__ = 'Tony Zhu'

from PyQt5.QtWidgets import QApplication, QWidget, QLineEdit, QInputDialog, QGridLayout, QLabel, QPushButton, QFrame

class InputDialog(QWidget):
  def __init__(self):    
    super(InputDialog,self).__init__()
    self.initUi()

  def initUi(self):
    self.setWindowTitle("    ")
    self.setGeometry(400,400,300,260)

    label1=QLabel("    :")
    label2=QLabel("    :")
    label3=QLabel("    :")
    label4=QLabel("    :")
    label5=QLabel("    :")

    self.nameLable = QLabel("PyQt5")
    self.nameLable.setFrameStyle(QFrame.Panel|QFrame.Sunken)
    self.styleLable = QLabel("  ")
    self.styleLable.setFrameStyle(QFrame.Panel|QFrame.Sunken)
    self.numberLable = QLabel("40")
    self.numberLable.setFrameStyle(QFrame.Panel|QFrame.Sunken)
    self.costLable = QLabel("400.98")
    self.costLable.setFrameStyle(QFrame.Panel|QFrame.Sunken)
    self.introductionLable = QLabel("         ")
    self.introductionLable.setFrameStyle(QFrame.Panel|QFrame.Sunken)

    nameButton=QPushButton("...")
    nameButton.clicked.connect(self.selectName)
    styleButton=QPushButton("...")
    styleButton.clicked.connect(self.selectStyle)
    numberButton=QPushButton("...")
    numberButton.clicked.connect(self.selectNumber)
    costButton=QPushButton("...")
    costButton.clicked.connect(self.selectCost)
    introductionButton=QPushButton("...")
    introductionButton.clicked.connect(self.selectIntroduction)

    mainLayout=QGridLayout()
    mainLayout.addWidget(label1,0,0)
    mainLayout.addWidget(self.nameLable,0,1)
    mainLayout.addWidget(nameButton,0,2)
    mainLayout.addWidget(label2,1,0)
    mainLayout.addWidget(self.styleLable,1,1)
    mainLayout.addWidget(styleButton,1,2)
    mainLayout.addWidget(label3,2,0)
    mainLayout.addWidget(self.numberLable,2,1)
    mainLayout.addWidget(numberButton,2,2)
    mainLayout.addWidget(label4,3,0)
    mainLayout.addWidget(self.costLable,3,1)
    mainLayout.addWidget(costButton,3,2)
    mainLayout.addWidget(label5,4,0)
    mainLayout.addWidget(self.introductionLable,4,1)
    mainLayout.addWidget(introductionButton,4,2)

    self.setLayout(mainLayout)



  def selectName(self):
    name,ok = QInputDialog.getText(self,"    ","      :",
                    QLineEdit.Normal,self.nameLable.text())
    if ok and (len(name)!=0):
      self.nameLable.setText(name)
  def selectStyle(self):
    list = ["  ","  "]

    style,ok = QInputDialog.getItem(self,"    ","       :",list)
    if ok :
      self.styleLable.setText(style)

  def selectNumber(self):
    number,ok = QInputDialog.getInt(self,"    ","         :",int(self.numberLable.text()),20,100,2)
    if ok :
      self.numberLable.setText(str(number))

  def selectCost(self):
    cost,ok = QInputDialog.getDouble(self,"    ","         :",float(self.costLable.text()),100.00,500.00,2)
    if ok :
      self.costLable.setText(str(cost))

  def selectIntroduction(self):
    introduction,ok = QInputDialog.getMultiLineText(self,"    ","  :","          
Python project") if ok : self.introductionLable.setText(introduction) if __name__=="__main__": import sys app=QApplication(sys.argv) myshow=InputDialog() myshow.show() sys.exit(app.exec_())
실행 후 효과:

예시 설명:
서로 다른 단 추 를 누 르 면 서로 다른 유형의 입력 대화 상 자 를 선택 하고 필요 한 데 이 터 를 선택 합 니 다.
코드 분석:
L18~22:

    label1=QLabel("    :")
    label2=QLabel("    :")
    label3=QLabel("    :")
    label4=QLabel("    :")
    label5=QLabel("    :")
데이터 항목 이름 의 탭 을 정의 합 니 다.
L24~33:

    self.nameLable = QLabel("PyQt5")
    self.nameLable.setFrameStyle(QFrame.Panel|QFrame.Sunken)
    self.styleLable = QLabel("  ")
    self.styleLable.setFrameStyle(QFrame.Panel|QFrame.Sunken)
    self.numberLable = QLabel("40")
    self.numberLable.setFrameStyle(QFrame.Panel|QFrame.Sunken)
    self.costLable = QLabel("400.98")
    self.costLable.setFrameStyle(QFrame.Panel|QFrame.Sunken)
    self.introductionLable = QLabel("         ")
    self.introductionLable.setFrameStyle(QFrame.Panel|QFrame.Sunken)
프로젝트 데이터 항목 의 데이터 내용 을 정의 하고 데이터 내용 은 해당 하 는 탭 에 표 시 됩 니 다.
setFrameStyle()탭 의 스타일 을 설정 합 니 다.다음 과 같은 스타일 이 있 습 니 다.
QFrame.Box
QFrame.Panel
QFrame.WinPanel
QFrame.HLine
QFrame.VLine
QFrame.StyledPanel
QFrame.Sunken
QFrame.Raised
L35~L44:

    nameButton=QPushButton("...")
    nameButton.clicked.connect(self.selectName)
    styleButton=QPushButton("...")
    styleButton.clicked.connect(self.selectStyle)
    numberButton=QPushButton("...")
    numberButton.clicked.connect(self.selectNumber)
    costButton=QPushButton("...")
    costButton.clicked.connect(self.selectCost)
    introductionButton=QPushButton("...")
    introductionButton.clicked.connect(self.selectIntroduction)
QPushButton 대상 을 예화 하고 해당 하 는 clicked 신호 와 사용자 정의 슬롯 함 수 를 연결 합 니 다.
L46~61:
격자 레이아웃 을 예화 하고 해당 하 는 컨트롤 을 격자 레이아웃 에 추가 합 니 다.
기능 분석:
1:항목 이름 가 져 오기:

  def selectName(self):
    name,ok = QInputDialog.getText(self,"    ","      :", QLineEdit.Normal,self.nameLable.text())
    if ok and (len(name)!=0):
      self.nameLable.setText(name)
QInputDialog 에는 정적 인 방법 이 많 기 때문에 예화 없 이 직접 호출 할 수 있 습 니 다.QInputDialog 의 getText()함수 팝 업 표준 문자열 입력 대화 상 자 를 호출 합 니 다.getText()함수 원형 은 다음 과 같 습 니 다.

 | getText(...)
 |   QInputDialog.getText(QWidget, str, str, QLineEdit.EchoMode echo=QLineEdit.Normal, str text=QString(), Qt.WindowFlags flags=0, Qt.InputMethodHints inputMethodHints=Qt.ImhNone) -> (str, bool)
첫 번 째 매개 변수 parent 는 부모 구성 요 소 를 지정 하 는 데 사 용 됩 니 다.
두 번 째 매개 변수 str 는 표준 입력 대화 상자 의 제목 이름 입 니 다.
세 번 째 매개 변수 str,표준 입력 대화 상자 의 태그 알림;
네 번 째 인자 echo,mode 는 표준 입력 대화 상자 의 QLine Edit 컨트롤 의 입력 모드 를 지정 합 니 다.
다섯 번 째 인자 str,표준 입력 대화 상자 의 QLine Edit 컨트롤 의 기본 값;
여섯 번 째 매개 변수 flags 는 표준 입력 대화 상자 의 창 표 지 를 가리킨다.
일곱 번 째 매개 변수 inpuutMethodHints 는 서로 다른 inpuutMethodHints 값 을 선택 하여 서로 다른 키보드 레이아웃 을 실현 합 니 다.
nameButton 을 누 른 후 효과:

사용자 가"OK"단 추 를 누 르 면 새로 입력 한 이름 을 표시 탭 으로 업데이트 합 니 다.
2:항목 속성 가 져 오기:

  def selectStyle(self):
    list = ["  ","  "]
    style,ok = QInputDialog.getItem(self,"    ","       :",list)
    if ok :
      self.styleLable.setText(style)
QInputDialog 의 getItem()함수 팝 업 표준 항목 선택 대화 상 자 를 호출 합 니 다.getItem()함수 원형 은 다음 과 같 습 니 다.

 | getItem(...)
 |   QInputDialog.getItem(QWidget, str, str, list-of-str, int current=0, bool editable=True, Qt.WindowFlags flags=0, Qt.InputMethodHints inputMethodHints=Qt.ImhNone) -> (str, bool)
첫 번 째 매개 변수 parent 는 부모 구성 요 소 를 지정 하 는 데 사 용 됩 니 다.
두 번 째 매개 변수 str 는 표준 항목 선택 대화 상자 의 제목 이름 입 니 다.
세 번 째 매개 변수 str,표준 항목 선택 대화 상자 의 탭 알림;
네 번 째 매개 변수 list-of-str,표준 항목 선택 대화 상자 에 해당 하 는 항목 의 list;
다섯 번 째 매개 변수 editable,표준 항목 은 대화 상자 항목 을 선택 하여 표 지 를 편집 할 수 있 는 지 여 부 를 선택 합 니 다.기본 값 은 편집 할 수 없습니다.
여섯 번 째 매개 변수 flags 는 표준 입력 대화 상자 의 창 표 지 를 가리킨다.
일곱 번 째 매개 변수 inpuutMethodHints 는 서로 다른 inpuutMethodHints 값 을 선택 하여 서로 다른 키보드 레이아웃 을 실현 합 니 다.
styleButton 을 누 른 후 효과:

사용자 가"OK"단 추 를 누 르 면 새로 선택 한 종 류 를 표시 탭 으로 업데이트 합 니 다.
3:프로젝트 구성원 가 져 오기:

  def selectNumber(self):
    number,ok = QInputDialog.getInt(self,"    ","         :",int(self.numberLable.text()),20,100,2)
    if ok :
      self.numberLable.setText(str(number))
QInputDialog 의 getInt()함수 팝 업 표준 int 형식 입력 대화 상 자 를 호출 합 니 다.getInt()함수 원형 은 다음 과 같 습 니 다.

| getInt(...)
|   QInputDialog.getInt(QWidget, str, str, int value=0, int min=-2147483647, int max=2147483647, int step=1, Qt.WindowFlags flags=0) -> (int, bool)
첫 번 째 매개 변수 parent 는 부모 구성 요 소 를 지정 하 는 데 사 용 됩 니 다.
두 번 째 매개 변수 str 는 표준 int 형식 입력 대화 상자 의 제목 이름 입 니 다.
세 번 째 매개 변수 str,표준 int 형식 입력 대화 상자 의 태그 알림;
네 번 째 매개 변수 value,표준 int 형식 입력 대화 상자 의 기본 값;
다섯 번 째 매개 변수 min,표준 int 형식 입력 대화 상자 의 최소 값;
여섯 번 째 매개 변수 max,표준 int 형식 입력 대화 상자 의 최대 값;
일곱 번 째 매개 변수 step,표준 int 형식 입력 대화 상자 의 보폭,즉 QSpinBox 의 상하 선택 은 데이터 변화의 보폭 입 니 다.
8 번 째 매개 변수 inpuutMethodHints 는 서로 다른 inpuutMethodHints 값 을 선택 하여 서로 다른 키보드 레이아웃 을 실현 합 니 다.
numberButton 을 누 른 후 효과:

사용자 가"OK"단 추 를 누 르 면 새로 선택 한 구성원 데 이 터 를 표시 탭 으로 업데이트 합 니 다.
4:프로젝트 원가 획득:

  def selectCost(self):
    cost,ok = QInputDialog.getDouble(self,"    ","         :",float(self.costLable.text()),100.00,500.00,2)
    if ok :
      self.costLable.setText(str(cost))
QInputDialog 의 getDouble()함수 팝 업 표준 float 형식 입력 대화 상 자 를 호출 합 니 다.getDouble()함수 원형 은 다음 과 같 습 니 다.

 | getDouble(...)
 |   QInputDialog.getDouble(QWidget, str, str, float value=0, float min=-2147483647, float max=2147483647, int decimals=1, Qt.WindowFlags flags=0) -> (float, bool)
첫 번 째 매개 변수 parent 는 부모 구성 요 소 를 지정 하 는 데 사 용 됩 니 다.
두 번 째 매개 변수 str,대화 상자 의 제목 을 입력 하 십시오.
세 번 째 매개 변수 str,대화 상자 의 탭 알림 입력;
네 번 째 매개 변수 value,표준 float 형식 입력 대화 상자 의 기본 값;
다섯 번 째 매개 변수 min,표준 float 형식 입력 대화 상자 의 최소 값;
여섯 번 째 매개 변수 max,표준 float 형식 입력 대화 상자 의 최대 값;
일곱 번 째 매개 변수 decimals,소수점 뒤에 남 아 있 는 자릿수;
8 번 째 매개 변수 inpuutMethodHints 는 서로 다른 inpuutMethodHints 값 을 선택 하여 서로 다른 키보드 레이아웃 을 실현 합 니 다.
costButton 을 누 른 후 효과:

사용자 가"OK"단 추 를 누 르 면 새로 선택 한 원가 데 이 터 를 디 스 플레이 탭 으로 업데이트 합 니 다.
5:항목 소개 가 져 오기:

  def selectIntroduction(self):
    introduction,ok = QInputDialog.getMultiLineText(self,"    ","  :","          
Python project") if ok : self.introductionLable.setText(introduction)
QInputDialog 의 getMultiLineText()함수 팝 업 표준 다 중 텍스트 형식 입력 대화 상 자 를 호출 합 니 다.getMultiLineText()함수 원형 은 다음 과 같 습 니 다.

 | getMultiLineText(...)
 |   QInputDialog.getMultiLineText(QWidget, str, str, str text='', Qt.WindowFlags flags=0, Qt.InputMethodHints inputMethodHints=Qt.ImhNone) -> (str, bool)
첫 번 째 매개 변수 parent 는 부모 구성 요 소 를 지정 하 는 데 사 용 됩 니 다.
두 번 째 매개 변수 str,대화 상자 의 제목 을 입력 하 십시오.
세 번 째 매개 변수 str,대화 상자 의 탭 알림 입력;
네 번 째 매개 변수 text,대화 상자 에서 Line Edit 의 기본 값 을 입력 하 십시오.
다섯 번 째 매개 변수 flags 는 표준 입력 대화 상자 의 창 표 지 를 가리킨다.
여섯 번 째 매개 변수 inpuutMethodHints 는 서로 다른 inpuutMethodHints 값 을 선택 하여 서로 다른 키보드 레이아웃 을 실현 합 니 다.
introductionButton 을 누 른 후 효과:

사용자 가"OK"단 추 를 누 르 면 새로 수 정 된 항목 소개 정 보 를 표시 탭 으로 업데이트 합 니 다.
이상 의 Pyqt 5 기본 인터페이스 구성 요소 의 inputDialog 사용 은 바로 작은 편집 이 여러분 에 게 공유 하 는 모든 내용 입 니 다.참고 하 시기 바 랍 니 다.여러분 들 도 저 희 를 많이 사랑 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기