框QDialog
方法描述
| setWindowTitle() | 設置對話框標題 |
| setWindowModality() | 設置窗口模態,取值如下:Qt.NonModal:非模態,可以和程序的其他窗口進行交互;Qt.WindowModal:窗口模態,程序在未處理玩當前對話框時,將阻止和對話框的父窗口進行交互;Qt.ApplicationModal:應用程序模態,阻止和任何其他窗口進行交互 |
import sys
from PyQt5
.QtCore
import *
from PyQt5
.QtGui
import *
from PyQt5
.QtWidgets
import *class DialogDemo(QMainWindow
):def __init__(self
, parent
=None):super(DialogDemo
, self
).__init__
(parent
)self
.setWindowTitle
("Dialog 例子")self
.resize
(350, 300)self
.btn
= QPushButton
(self
)self
.btn
.setText
("彈出對話框")self
.btn
.move
(50, 50)self
.btn
.clicked
.connect
(self
.showdialog
)def showdialog(self
):dialog
= QDialog
()btn
= QPushButton
("ok", dialog
)btn
.move
(50, 50)dialog
.setWindowTitle
("Dialog")dialog
.setWindowModality
(Qt
.ApplicationModal
)dialog
.exec_
()
if __name__
== '__main__':app
= QApplication
(sys
.argv
)demo
= DialogDemo
()demo
.show
()sys
.exit
(app
.exec_
())
彈出信息框QMessageBox
QMessageBox.information 信息框
QMessageBox.question 問答框
QMessageBox.warning 警告
QMessageBox.ctitical危險
QMessageBox.about 關于
#消息框
#self 當前窗口的父窗口
#消息:信息
QMessageBox.information(self,“消息框標題”,“這是一條消息。”,QMessageBox.Yes | QMessageBox.No)
#消息:問答
QMessageBox.question(self,“消息框標題”,“這是一條問答。”,QMessageBox.Yes | QMessageBox.No)
#消息:警告warning
QMessageBox.warning(self,“消息框標題”,“這是一條警告。”,QMessageBox.Yes | QMessageBox.No)
#消息:危險ctitical
QMessageBox.ctitical(self,“消息框標題”,“這是一條危險。”,QMessageBox.Yes | QMessageBox.No)
#消息:關于
QMessageBox.about(self,“消息框標題”,“這是關于對話框的說明”,QMessageBox.Yes | QMessageBox.No)
import sys
from PyQt5
.QtCore
import *
from PyQt5
.QtGui
import *
from PyQt5
.QtWidgets
import *class WinForm(QWidget
):def __init__(self
):super(WinForm
, self
).__init__
()self
.setWindowTitle
("QMessageBox 例子")self
.resize
(300, 100)self
.myButton
= QPushButton
(self
)self
.myButton
.setText
("點擊彈出消息框")self
.myButton
.clicked
.connect
(self
.msg
)def msg(self
):reply
= QMessageBox
.information
(self
, "標題", "對話框消息正文", QMessageBox
.Yes
| QMessageBox
.No
, QMessageBox
.Yes
)print(reply
)if __name__
== '__main__':app
= QApplication
(sys
.argv
)demo
= WinForm
()demo
.show
()sys
.exit
(app
.exec_
())
輸入框QInputDialog
QInputDialog控件是一個標準對話框,有一個文本框和兩個按鈕(ok和cancel)組成,當用戶單擊ok或enter鍵后,在父窗口可以收集通過QInputDialog控件輸入的信息,QInputDialog控件是QDialog標準對話框的一部分
在QInpuTDialog控件中可以輸入數字,字符串或列表中的選項,標簽用于提示必要的信息
方法描述
| getint() | 從控件中獲得標準整數輸入 |
| getDouble() | 從控件中獲得標準浮點數輸入 |
| getText() | 從控件中獲得標準字符串的輸入 |
| getItem() | 從控件中獲得列表里的選項輸入 |
import sys
from PyQt5
.QtCore
import *
from PyQt5
.QtGui
import *
from PyQt5
.QtWidgets
import *class InputdialogDemo(QWidget
):def __init__(self
, parent
=None):super(InputdialogDemo
, self
).__init__
(parent
)layout
= QFormLayout
()self
.btn1
= QPushButton
("獲得列表里的選項")self
.btn1
.clicked
.connect
(self
.getItem
)self
.le1
= QLineEdit
()layout
.addRow
(self
.btn1
, self
.le1
)self
.btn2
= QPushButton
("獲得字符串")self
.btn2
.clicked
.connect
(self
.getIext
)self
.le2
= QLineEdit
()layout
.addRow
(self
.btn2
, self
.le2
)self
.btn3
= QPushButton
("獲得整數")self
.btn3
.clicked
.connect
(self
.getInt
)self
.le3
= QLineEdit
()layout
.addRow
(self
.btn3
, self
.le3
)self
.setLayout
(layout
)self
.setWindowTitle
("Input Dialog 例子")def getItem(self
):items
= ("C", "C++", "Java", "Python")item
, ok
= QInputDialog
.getItem
(self
, "select input dialog","語言列表", items
, 0, False)if ok
and item
:self
.le1
.setText
(item
)def getIext(self
):text
, ok
= QInputDialog
.getText
(self
, 'Text Input Dialog', '輸入姓名:')if ok
:self
.le2
.setText
(str(text
))def getInt(self
):num
, ok
= QInputDialog
.getInt
(self
, "integer input dualog", "輸入數字")if ok
:self
.le3
.setText
(str(num
))if __name__
== '__main__':app
= QApplication
(sys
.argv
)demo
= InputdialogDemo
()demo
.show
()sys
.exit
(app
.exec_
())
QInputDialog.getText(self, ‘窗口名’, ‘提升信息:’)
初始界面
點擊左邊按鈕,彈出對話框界面
在對話框里輸入內容,點擊OK,就會顯示在頁面上。
字體框QFontDialog
import sys
from PyQt5
.QtCore
import *
from PyQt5
.QtGui
import *
from PyQt5
.QtWidgets
import *class FontDialogDemo(QWidget
):def __init__(self
, parent
=None):super(FontDialogDemo
, self
).__init__
(parent
)layout
= QVBoxLayout
()self
.fontButton
= QPushButton
("choose font")self
.fontButton
.clicked
.connect
(self
.getFont
)layout
.addWidget
(self
.fontButton
)self
.fontLineEdit
= QLabel
("Hello,測試字體例子")layout
.addWidget
(self
.fontLineEdit
)self
.setLayout
(layout
)self
.setWindowTitle
("Font Dialog 例子")def getFont(self
):font
, ok
= QFontDialog
.getFont
()if ok
:self
.fontLineEdit
.setFont
(font
)
if __name__
== '__main__':app
= QApplication
(sys
.argv
)demo
= FontDialogDemo
()demo
.show
()sys
.exit
(app
.exec_
())
文件框QFileDialog
選取文件夾 QFileDialog.getExistingDirectory()
選擇文件 QFileDialog.getOpenFileName()
選擇多個文件 QFileDialog.getOpenFileNames()
選擇保存文件 QFileDialog.getSaveFileName()
方法描述
| getOpenFileName() | 返回用戶所選擇文件的名稱,并打開該文件 |
| getSaveFileName() | 使用用戶選擇的文件名保存文件 |
| setFileMode() | 可以選擇的文件類型,枚舉常量是: |
| - | QFileDialog.AnyFile:任何文件 |
| - | QFileDialog.ExistingFile:已存在的文件 |
| - | QFileDialog.Directory:文件目錄 |
| - | QFileDialog.ExistingFiles:已經存在的多個文件 |
| setFilter() | 設置過濾器,只顯示過濾器允許的文件類型 |
import sys
from PyQt5
.QtCore
import *
from PyQt5
.QtGui
import *
from PyQt5
.QtWidgets
import *class filedialogdemo(QWidget
):def __init__(self
, parent
=None):super(filedialogdemo
, self
).__init__
(parent
)layout
= QVBoxLayout
()self
.btn
= QPushButton
("加載圖片")self
.btn
.clicked
.connect
(self
.getfile
)layout
.addWidget
(self
.btn
)self
.le
= QLabel
("")layout
.addWidget
(self
.le
)self
.btn1
= QPushButton
("加載文本文件")self
.btn1
.clicked
.connect
(self
.getfiles
)layout
.addWidget
(self
.btn1
)self
.contents
= QTextEdit
()layout
.addWidget
(self
.contents
)self
.setLayout
(layout
)self
.setWindowTitle
("File Dialog 例子")def getfile(self
):fname
, _
= QFileDialog
.getOpenFileName
(self
, 'Open file', 'c:\\', "Image files (*.jpg *.gif)")self
.le
.setPixmap
(QPixmap
(fname
))def getfiles(self
):dlg
= QFileDialog
()dlg
.setFileMode
(QFileDialog
.AnyFile
)dlg
.setFilter
(QDir
.Files
)if dlg
.exec_
():filenames
= dlg
.selectedFiles
()f
= open(filenames
[0], 'r')with f
:data
= f
.read
()self
.contents
.setText
(data
)
if __name__
== '__main__':app
= QApplication
(sys
.argv
)ex
= filedialogdemo
()ex
.show
()sys
.exit
(app
.exec_
())
fname, _ = QFileDialog.getOpenFileName(self, ‘Open file’, ‘c:\’, “Image files (*.jpg .gif)")
fname:文件名
_:打開的文件類型
‘Open file’:打開窗口名,可以不寫。設為""
‘c:\’:初始打開文件夾路徑
Image files (.jpg *.gif)”:文件過濾,
All Files(*) 所有類型文件。
imgName, imgType = QFileDialog.getOpenFileName(self, "打開圖片", "", "*.jpg;;*.png;;All Files(*)")
電氣工程的計算機萌新,寫博文不容易。如果你覺得本文對你有用,請點個贊支持下。謝謝。
總結
以上是生活随笔為你收集整理的pyqt5讲解4:QDialog,QMessageBox,QInputDialog,QFontDialog,QFileDialog的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。