子類主要有QMessageBox、QFileDialog、QFontDialog、QInputDialog等
創(chuàng)新互聯(lián),專注為中小企業(yè)提供官網(wǎng)建設(shè)、營(yíng)銷(xiāo)型網(wǎng)站制作、自適應(yīng)網(wǎng)站建設(shè)、展示型網(wǎng)站制作、成都網(wǎng)站設(shè)計(jì)等服務(wù),幫助中小企業(yè)通過(guò)網(wǎng)站體現(xiàn)價(jià)值、有效益。幫助企業(yè)快速建站、解決網(wǎng)站建設(shè)與網(wǎng)站營(yíng)銷(xiāo)推廣問(wèn)題。
測(cè)試代碼
# -*- coding:utf8 -*-
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QMainWindow, QApplication
class MyDialog(QMainWindow):
def __init__(self, parent=None):
super(MyDialog, self).__init__(parent)
# 設(shè)置窗口標(biāo)題
self.setWindowTitle("測(cè)試窗口")
# 設(shè)置窗口大小
self.resize(350, 300)
# 設(shè)置窗口為非模態(tài)
self.setWindowModality(Qt.NonModal)
# 設(shè)置窗口為窗口模態(tài),程序在未處理完當(dāng)前對(duì)話框時(shí)將阻止和對(duì)話框的父窗口進(jìn)行交互
self.setWindowModality(Qt.WindowModal)
# 設(shè)置窗口為應(yīng)用程序模態(tài),阻止和人任何其他窗口進(jìn)行交互
self.setWindowModality(Qt.ApplicationModal)
if __name__ == '__main__':
app = QApplication(sys.argv)
ui_demo = MyDialog()
ui_demo.show()
sys.exit(app.exec_())
對(duì)話框函數(shù)
# 信息提示對(duì)話框
QMessageBox.information(ParentQWidght, Title, Text, Buttons, DefaultButton)
# 問(wèn)題響應(yīng)對(duì)話框
QMessageBox.question(ParentQWidght, Title, Text, Buttons, DefaultButton)
# 警告對(duì)話框
QMessageBox.warning(ParentQWidght, Title, Text, Buttons, DefaultButton)
# 關(guān)于對(duì)話框
QMessageBox.about(ParentQWidght, Title, Text, Buttons, DefaultButton)
# 嚴(yán)重警告對(duì)話框
QMessageBox.critical(ParentQWidght, Title, Text, Buttons, DefaultButton)
PyQt5源碼中定義的按鈕
# 調(diào)用方法:QMessage.xxx
Abort = 262144
AcceptRole = 0
ActionRole = 3
Apply = 33554432
ApplyRole = 8
ButtonMask = -769
Cancel = 4194304
Close = 2097152
Critical = 3
Default = 256
DestructiveRole = 2
Discard = 8388608
Escape = 512
FirstButton = 1024
FlagMask = 768
Help = 16777216
HelpRole = 4
Ignore = 1048576
Information = 1
InvalidRole = -1
LastButton = 134217728
No = 65536
NoAll = 131072
NoButton = 0
NoIcon = 0
NoRole = 6
NoToAll = 131072
Ok = 1024
Open = 8192
Question = 4
RejectRole = 1
Reset = 67108864
ResetRole = 7
RestoreDefaults = 134217728
Retry = 524288
Save = 2048
SaveAll = 4096
Warning = 2
Yes = 16384
YesAll = 32768
YesRole = 5
YesToAll = 32768
測(cè)試代碼
from PyQt5.QtWidgets import QMessageBox
# 僅僅顯示
if(msg=='connect success'):
conn.close()
QMessageBox.information(self, "溫馨提示", "數(shù)據(jù)庫(kù)連接成功!", QMessageBox.Yes, QMessageBox.Yes)
else:
QMessageBox.warning(self, "警告信息", "由于目標(biāo)計(jì)算機(jī)積極拒絕, 無(wú)法連接, 來(lái)自數(shù)據(jù)庫(kù)的消息:{0}".format(msg), QMessageBox.Yes, QMessageBox.Yes)
# 處理用戶應(yīng)答后的動(dòng)作
reply = QMessageBox.warning(self, "溫馨提示", "即將退出, 確定?", QMessageBox.Yes|QMessageBox.No, QMessageBox.No)
if(reply == QMessageBox.Yes):
QCloseEvent.accept()
if(reply==QMessageBox.No):
QCloseEvent.ignore()
常用取值函數(shù)
# 獲取實(shí)數(shù)
QInputDialog.getDouble(ParentQWidght,Title,Text)
# 獲取整數(shù)
QInputDialog.getInt(ParentQWidght,Title,Text)
# 在控件中的獲得列表中獲取輸入
QInputDialog.getItem(ParentQWidght,Title,Text,DefaultItem, isOperation)
# 獲取文本
QInputDialog.getText(ParentQWidght,Title,Text)
# 獲取多行文本
QInputDialog.getMultiLineText(ParentQWidght,Title,Text,DefaultText)
測(cè)試代碼
from PyQt5.QtWidgets import QInputDialog
number, ok = QInputDialog.getInt(self, "輸入數(shù)字對(duì)話框", "請(qǐng)輸入數(shù)字")
if(ok):
print(number)
number, ok = QInputDialog.getDouble(self, "輸入數(shù)字對(duì)話框", "請(qǐng)輸入數(shù)字")
if(ok):
print(number)
text, ok = QInputDialog.getText(self, "輸入數(shù)字對(duì)話框", "請(qǐng)輸入數(shù)字")
if(ok):
print(text)
mul_text, ok = QInputDialog.getMultiLineText(self, "xixi", "haha", "hehe")
if(ok):
print(mul_text)
item, ok = QInputDialog.getItem(self, "", "", ['One', 'Two', 'Three'], 0, False)
if(ok):
print(item)
測(cè)試代碼
from PyQt5.QtWidgets import QFontDialog
font, ok = QFontDialog.getFont()
if(ok):
# 設(shè)置字體樣式函數(shù)
self.lintEdit.setFont(font)
測(cè)試代碼
from PyQt5.QtGui import QPalette
from PyQt5.QtWidgets import QColorDialog
color = QColorDialog.getColor()
if(color.isValid()):
# 設(shè)置顏色名稱
self.label.setText(color.name())
# 設(shè)置顏色
self.label.setPalette(QPalette(color))
# 設(shè)置自動(dòng)填充背景顏色
self.label.setAutoFillBackground(True)
QFileDialog文件選擇對(duì)話框
常用函數(shù)
# 獲取文件的打開(kāi)路徑
QFileDialog.getOpenFileName()
# 獲取一組文件的打開(kāi)路徑
QFileDialog.getOpenFileNames()
# 獲取文件的保存路徑
QFileDialog.getSaveFileName()
# 設(shè)置可以選擇的文件類型
QFileDialog.setFileMode()
"""枚舉常量包括:
QFileDialog.AnyFile
QFileDialog.ExistingFile
QFileDialog.Directory
QFileDialog.ExistingFiles
"""
# 設(shè)置文件過(guò)濾器,顯示的文件類型
QFileDialog.setFilter()
# 獲取已經(jīng)存在的目錄
QFileDialog.getExistingDirectory()
測(cè)試代碼
file_name, ok = QFileDialog.getOpenFileName(self, "打開(kāi)文件", '/', "Images files (*.jpg *.gif)")
if(ok):
print(file_name)
files_name, ok = QFileDialog.getOpenFileNames(self, "打開(kāi)一組文件", '/', "All Files (*);;Text Files (*.txt)")
if(ok):
for i in files_name:
print(i)
file_name, ok = QFileDialog.getSaveFileName(self, "保存文件", '/', "Images files (*.jpg *.gif)")
if(ok):
print(files_name)
QFileDialog.setFileMode(QFileDialog.AnyFile)
QFileDialog.setFilter("Images files (*.jpg *.gif)")
path = QFileDialog.getExistingDirectory(self, "選擇目錄", '/')
print(path)