顯示基本窗體子類主要有QMessageBox、QFileDialog、QFontDialog、QInputDialog等
成都創(chuàng)新互聯(lián)專注于寬城企業(yè)網(wǎng)站建設,響應式網(wǎng)站,電子商務商城網(wǎng)站建設。寬城網(wǎng)站建設公司,為寬城等地區(qū)提供建站服務。全流程專業(yè)公司,專業(yè)設計,全程項目跟蹤,成都創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務
測試代碼
# -*- 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)
# 設置窗口為非模態(tài)
self.setWindowModality(Qt.NonModal)
# 設置窗口為窗口模態(tài),程序在未處理完當前對話框時將阻止和對話框的父窗口進行交互
self.setWindowModality(Qt.WindowModal)
# 設置窗口為應用程序模態(tài),阻止和人任何其他窗口進行交互
self.setWindowModality(Qt.ApplicationModal)
if __name__ == '__main__':
app = QApplication(sys.argv)
ui_demo = MyDialog()
ui_demo.show()
sys.exit(app.exec_())
QMessageBox通用式彈出對話框對話框函數(shù)
# 信息提示對話框
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, "溫馨提示", "數(shù)據(jù)庫連接成功!", QMessageBox.Yes, QMessageBox.Yes)
else:
QMessageBox.warning(self, "警告信息", "由于目標計算機積極拒絕, 無法連接, 來自數(shù)據(jù)庫的消息:{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標準輸入對話框常用取值函數(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)
測試代碼
from PyQt5.QtWidgets import QInputDialog
number, ok = QInputDialog.getInt(self, "輸入數(shù)字對話框", "請輸入數(shù)字")
if(ok):
print(number)
number, ok = QInputDialog.getDouble(self, "輸入數(shù)字對話框", "請輸入數(shù)字")
if(ok):
print(number)
text, ok = QInputDialog.getText(self, "輸入數(shù)字對話框", "請輸入數(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)
QFontDialog字體選擇對話框測試代碼
from PyQt5.QtWidgets import QFontDialog
font, ok = QFontDialog.getFont()
if(ok):
# 設置字體樣式函數(shù)
self.lintEdit.setFont(font)
QColorDialog顏色選取對話框測試代碼
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文件選擇對話框
常用函數(shù)
# 獲取文件的打開路徑
QFileDialog.getOpenFileName()
# 獲取一組文件的打開路徑
QFileDialog.getOpenFileNames()
# 獲取文件的保存路徑
QFileDialog.getSaveFileName()
# 設置可以選擇的文件類型
QFileDialog.setFileMode()
"""枚舉常量包括:
QFileDialog.AnyFile
QFileDialog.ExistingFile
QFileDialog.Directory
QFileDialog.ExistingFiles
"""
# 設置文件過濾器,顯示的文件類型
QFileDialog.setFilter()
# 獲取已經(jīng)存在的目錄
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)