QDialog對話框:QDialog是對話框的父類,子類大概有:QMessageBox、QFileDialog、QFontDialog、QInputDialog等等。
常用方法
setWindowTitle() |
設置對話框標題 |
setWindowModality() |
設置窗口模態 Qt.NonMadal: 非模態,可以和程序的其他窗口交互 Qt.WindowModal: 窗口模態,程序在未處理完當前對話框時,將阻止和對話框的父窗口進行交互 Qt.ApplicationModal: 應用程序模態,阻止和任何其他窗口進行交互 |
#對話框QDialog實例
# -*- coding: utf-8 -*-
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(QWidget parent,title,text,buttons,defaultButton) |
彈出消息對話框,參數解釋如下: parent:指定的父窗口控件 title:對話框標題 text:對話框文本 buttons:多個標準按鈕,默認爲ok defaultButton:默認第一個標準按鈕 |
question(QWidget parent,title,text,buttons,defaultButton) |
彈出問答對話框 |
warning(Widget parent,title,text,buttons,defaultButton) |
彈出警告對話框 |
ctitical(QWidget parent,title,text,buttons,defaultButton) |
彈出嚴重錯誤對話框 |
about(QWidget parent,title,text) |
彈出關於對話框 |
setTitle() |
設置標題 |
setText() |
設置消息正文 |
setIcon() |
設置對話框圖片 |
標準按鈕類型
QMessage.Ok |
同意 |
QMessage.Cancel |
取消 |
QMessage.Yes |
同意 |
QMessage.No |
取消 |
QMessage.Abort |
終止 |
QMessage.Retry |
重試 |
QMessage.Ignore |
忽略 |
五種常用對話框及顯示效果
- QMessageBox.information(self,"標題","消息對話框正文",QMessageBox.Yes|QMessage.No,QMessage.Yes)
- QmessageBox.question(self,"標題","提問對話框正文",QMessageBox.Yes|QMessage.No,QMessage.Yes)
- QmessageBox.warning(self,"標題","警告對話框正文",QMessageBox.Yes|QMessage.No,QMessage.Yes)
- QmessageBox.critical(self,"標題","嚴重錯誤對話框正文",QMessageBox.Yes|QMessage.No,QMessage.Yes)
- QmessageBox.about(self,"標題","關於對話框正文",QMessageBox.Yes|QMessage.No,QMessage.Yes)
#消息對話框對話框QMessageBox實例
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class MyWindow(QWidget):
def __init__(self,parent=None):
super(MyWindow, self).__init__(parent)
self.setWindowTitle("消息對話框")
self.resize(300,100)
self.initUI()
def initUI(self):
self.myButton=QPushButton(self)
self.myButton.setText("點擊彈出對話框")
self.myButton.clicked.connect(self.msg)
def msg(self):
#使用infomation信息框
reply=QMessageBox.information(self,"標題","消息正文",QMessageBox.Yes|QMessageBox.No,QMessageBox.No)
print(reply)
if __name__=="__main__":
app=QApplication(sys.argv)
myShow=MyWindow()
myShow.show()
sys.exit(app.exec_())
QInputDialog:該控件是一個標準對話框,有一個文本框和兩個按鈕組成(ok、cancel)
常用方法
getInt() |
獲得標準整數輸入 |
getDoublie() |
獲得標準浮點數輸入 |
getText() |
獲得標準字符串輸入 |
getItem() |
獲得列表裏的選項輸入 |
#消息對話框對話框QMessageBox實例
# -*- coding: utf-8 -*-
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)
self.setWindowTitle("QInputDialog")
self.layout=QFormLayout()
self.initUI()
def initUI(self):
self.btn1=QPushButton("獲得列表裏的選項")
self.btn1.clicked.connect(self.getItem)
self.le1=QLineEdit()
self.layout.addRow(self.btn1,self.le1)
self.btn2=QPushButton("獲得字符串")
self.btn2.clicked.connect(self.getIext)
self.le2=QLineEdit()
self.layout.addRow(self.btn2,self.le2)
self.btn3=QPushButton("獲得整數")
self.btn3.clicked.connect(self.getInt)
self.le3=QLineEdit()
self.layout.addRow(self.btn3,self.le3)
self.setLayout(self.layout)
def getItem(self):
items=("C","C++","Java","Python")
#從列表裏選擇一個選項,點擊OK鍵,最終選到的值即賦值給item
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 dialog","輸入數字")
if ok:
self.le3.setText(str(num))
if __name__=="__main__":
app=QApplication(sys.argv)
demo=InputDialogDemo()
demo.show()
sys.exit(app.exec_())
QFontDialog:該控件是一個常用的字體選擇對話框,可以讓用戶選擇所顯示的文本的字號大小、樣式、和格式。使用getFont()可以從字體選擇對話框中選擇文本顯示字號、大小、樣式、格式。
#消息對話框對話框QFontDialog實例
# -*- coding: utf-8 -*-
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 Diaog例子")
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在打開文件時使用了文件過濾器,用於顯示指定的擴展名的文件
常用方法
getOpenFileName() |
返回用戶所選擇文件的名稱,並打開該文件 |
getSaveFileName() |
使用用戶選擇的文件名並保存文件 |
setFileMode() |
可選擇的文件類型: QFileDialog.AnyFile 任何文件 QFileDialog.ExistingFile 已存在的文件 QFileDialog.Directory文件目錄 QFileDialog.ExistingFiles已存在的多個文件 |
setFilter() |
設置過濾器,只顯示過濾器允許的文件類型 |
#消息對話框對話框QFileDialog實例
# -*- coding: utf-8 -*-
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("FileDialog")
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)
demo=FileDialogDemo()
demo.show()
sys.exit(app.exec_())