您好,登錄后才能下訂單哦!
子類主要有QMessageBox、QFileDialog、QFontDialog、QInputDialog等
測試代碼
# -*- 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)
# 設置窗口標題
self.setWindowTitle("測試窗口")
# 設置窗口大小
self.resize(350, 300)
# 設置窗口為非模態
self.setWindowModality(Qt.NonModal)
# 設置窗口為窗口模態,程序在未處理完當前對話框時將阻止和對話框的父窗口進行交互
self.setWindowModality(Qt.WindowModal)
# 設置窗口為應用程序模態,阻止和人任何其他窗口進行交互
self.setWindowModality(Qt.ApplicationModal)
if __name__ == '__main__':
app = QApplication(sys.argv)
ui_demo = MyDialog()
ui_demo.show()
sys.exit(app.exec_())
對話框函數
# 信息提示對話框
QMessageBox.information(ParentQWidght, Title, Text, Buttons, DefaultButton)
# 問題響應對話框
QMessageBox.question(ParentQWidght, Title, Text, Buttons, DefaultButton)
# 警告對話框
QMessageBox.warning(ParentQWidght, Title, Text, Buttons, DefaultButton)
# 關于對話框
QMessageBox.about(ParentQWidght, Title, Text, Buttons, DefaultButton)
# 嚴重警告對話框
QMessageBox.critical(ParentQWidght, Title, Text, Buttons, DefaultButton)
PyQt5源碼中定義的按鈕
# 調用方法: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
測試代碼
from PyQt5.QtWidgets import QMessageBox
# 僅僅顯示
if(msg=='connect success'):
conn.close()
QMessageBox.information(self, "溫馨提示", "數據庫連接成功!", QMessageBox.Yes, QMessageBox.Yes)
else:
QMessageBox.warning(self, "警告信息", "由于目標計算機積極拒絕, 無法連接, 來自數據庫的消息:{0}".format(msg), QMessageBox.Yes, QMessageBox.Yes)
# 處理用戶應答后的動作
reply = QMessageBox.warning(self, "溫馨提示", "即將退出, 確定?", QMessageBox.Yes|QMessageBox.No, QMessageBox.No)
if(reply == QMessageBox.Yes):
QCloseEvent.accept()
if(reply==QMessageBox.No):
QCloseEvent.ignore()
常用取值函數
# 獲取實數
QInputDialog.getDouble(ParentQWidght,Title,Text)
# 獲取整數
QInputDialog.getInt(ParentQWidght,Title,Text)
# 在控件中的獲得列表中獲取輸入
QInputDialog.getItem(ParentQWidght,Title,Text,DefaultItem, isOperation)
# 獲取文本
QInputDialog.getText(ParentQWidght,Title,Text)
# 獲取多行文本
QInputDialog.getMultiLineText(ParentQWidght,Title,Text,DefaultText)
測試代碼
from PyQt5.QtWidgets import QInputDialog
number, ok = QInputDialog.getInt(self, "輸入數字對話框", "請輸入數字")
if(ok):
print(number)
number, ok = QInputDialog.getDouble(self, "輸入數字對話框", "請輸入數字")
if(ok):
print(number)
text, ok = QInputDialog.getText(self, "輸入數字對話框", "請輸入數字")
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)
測試代碼
from PyQt5.QtWidgets import QFontDialog
font, ok = QFontDialog.getFont()
if(ok):
# 設置字體樣式函數
self.lintEdit.setFont(font)
測試代碼
from PyQt5.QtGui import QPalette
from PyQt5.QtWidgets import QColorDialog
color = QColorDialog.getColor()
if(color.isValid()):
# 設置顏色名稱
self.label.setText(color.name())
# 設置顏色
self.label.setPalette(QPalette(color))
# 設置自動填充背景顏色
self.label.setAutoFillBackground(True)
QFileDialog文件選擇對話框
常用函數
# 獲取文件的打開路徑
QFileDialog.getOpenFileName()
# 獲取一組文件的打開路徑
QFileDialog.getOpenFileNames()
# 獲取文件的保存路徑
QFileDialog.getSaveFileName()
# 設置可以選擇的文件類型
QFileDialog.setFileMode()
"""枚舉常量包括:
QFileDialog.AnyFile
QFileDialog.ExistingFile
QFileDialog.Directory
QFileDialog.ExistingFiles
"""
# 設置文件過濾器,顯示的文件類型
QFileDialog.setFilter()
# 獲取已經存在的目錄
QFileDialog.getExistingDirectory()
測試代碼
file_name, ok = QFileDialog.getOpenFileName(self, "打開文件", '/', "Images files (*.jpg *.gif)")
if(ok):
print(file_name)
files_name, ok = QFileDialog.getOpenFileNames(self, "打開一組文件", '/', "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)
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。