日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

pyqt5教程8:对话框

發布時間:2025/3/21 编程问答 14 豆豆
生活随笔 收集整理的這篇文章主要介紹了 pyqt5教程8:对话框 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Dialogs in PyQt5

對話被定義為兩個或多個人之間的對話。在計算機應用程序中,對話框是用于與應用程序“對話”的窗口。對話框用于諸如從用戶獲取數據或更改應用程序設置之類的事情。

1 PyQt5 輸入對話框QInputDialog

QInputDialog 提供了一個簡單方便的對話框來從用戶那里獲取單個值。輸入值可以是字符串、數字或列表中的項目。

input_dialog.py

#!/usr/bin/python""" ZetCode PyQt5 tutorialIn this example, we receive data from a QInputDialog dialog.Aauthor: Jan Bodnar Website: zetcode.com """from PyQt5.QtWidgets import (QWidget, QPushButton, QLineEdit,QInputDialog, QApplication) import sysclass Example(QWidget):def __init__(self):super().__init__()self.initUI()def initUI(self):self.btn = QPushButton('Dialog', self)self.btn.move(20, 20)self.btn.clicked.connect(self.showDialog)self.le = QLineEdit(self)self.le.move(130, 22)self.setGeometry(300, 300, 450, 350)self.setWindowTitle('Input dialog')self.show()def showDialog(self):text, ok = QInputDialog.getText(self, 'Input Dialog','Enter your name:')if ok:self.le.setText(str(text))def main():app = QApplication(sys.argv)ex = Example()sys.exit(app.exec_())if __name__ == '__main__':main()

該示例有一個按鈕和一個行編輯小部件。該按鈕顯示用于獲取文本值的輸入對話框。輸入的文本將顯示在行編輯小部件中。

text, ok = QInputDialog.getText(self, 'Input Dialog','Enter your name:')

此行顯示輸入對話框。第一個字符串是對話框標題,第二個字符串是對話框中的消息。對話框返回輸入的文本和一個布爾值。如果我們單擊 Ok 按鈕,則布爾值為 true。

if ok:self.le.setText(str(text))

我們從對話框中收到的文本被設置為帶有 setText 的行編輯小部件。

Figure: Input dialog

1.2 顏色選擇對話框QColorDialog

QColorDialog 提供了一個用于選擇顏色值的對話框小部件。

color_dialog.py

#!/usr/bin/python""" ZetCode PyQt5 tutorialIn this example, we select a color value from the QColorDialog and change the background color of a QFrame widget.Author: Jan Bodnar Website: zetcode.com """from PyQt5.QtWidgets import (QWidget, QPushButton, QFrame,QColorDialog, QApplication) from PyQt5.QtGui import QColor import sysclass Example(QWidget):def __init__(self):super().__init__()self.initUI()def initUI(self):col = QColor(0, 0, 0)self.btn = QPushButton('Dialog', self)self.btn.move(20, 20)self.btn.clicked.connect(self.showDialog)self.frm = QFrame(self)self.frm.setStyleSheet("QWidget { background-color: %s }"% col.name())self.frm.setGeometry(130, 22, 200, 200)self.setGeometry(300, 300, 450, 350)self.setWindowTitle('Color dialog')self.show()def showDialog(self):col = QColorDialog.getColor()if col.isValid():self.frm.setStyleSheet('QWidget { background-color: %s }'% col.name())def main():app = QApplication(sys.argv)ex = Example()sys.exit(app.exec_())if __name__ == '__main__':main()

應用示例顯示了一個按鈕和一個 QFrame。小部件背景設置為黑色。使用 QColorDialog,我們可以改變它的背景。

col = QColor(0, 0, 0)

這是 QFrame 背景的初始顏色。

col = QColorDialog.getColor()

This line pops up the?QColorDialog.

if col.isValid():self.frm.setStyleSheet("QWidget { background-color: %s }"% col.name())

我們檢查顏色是否有效。如果我們單擊取消按鈕,則不會返回有效顏色。如果顏色有效,我們使用樣式表更改背景顏色。

1.3 字體選擇QFontDialog

QFontDialog?is a dialog widget for selecting a font.

font_dialog.py

#!/usr/bin/python""" ZetCode PyQt5 tutorialIn this example, we select a font name and change the font of a label.Author: Jan Bodnar Website: zetcode.com """from PyQt5.QtWidgets import (QWidget, QVBoxLayout, QPushButton,QSizePolicy, QLabel, QFontDialog, QApplication) import sysclass Example(QWidget):def __init__(self):super().__init__()self.initUI()def initUI(self):vbox = QVBoxLayout()btn = QPushButton('Dialog', self)btn.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)btn.move(20, 20)vbox.addWidget(btn)btn.clicked.connect(self.showDialog)self.lbl = QLabel('Knowledge only matters', self)self.lbl.move(130, 20)vbox.addWidget(self.lbl)self.setLayout(vbox)self.setGeometry(300, 300, 450, 350)self.setWindowTitle('Font dialog')self.show()def showDialog(self):font, ok = QFontDialog.getFont()if ok:self.lbl.setFont(font)def main():app = QApplication(sys.argv)ex = Example()sys.exit(app.exec_())if __name__ == '__main__':main()

在我們的示例中,我們有一個按鈕和一個標簽。使用 QFontDialog,我們可以更改標簽的字體。

font, ok = QFontDialog.getFont()

這里我們彈出字體對話框。 getFont 方法返回字體名稱和 ok 參數。如果用戶點擊 Ok 則等于 True;否則為假。

if ok:self.label.setFont(font)

如果我們點擊確定,標簽的字體會使用 setFont 進行更改。

1.4 文件操作QFileDialog

QFileDialog 是一個允許用戶選擇文件或目錄的對話框。可以選擇文件進行打開和保存。

file_dialog.py

#!/usr/bin/python""" ZetCode PyQt5 tutorialIn this example, we select a file with a QFileDialog and display its contents in a QTextEdit.Author: Jan Bodnar Website: zetcode.com """from PyQt5.QtWidgets import (QMainWindow, QTextEdit,QAction, QFileDialog, QApplication) from PyQt5.QtGui import QIcon import sys from pathlib import Pathclass Example(QMainWindow):def __init__(self):super().__init__()self.initUI()def initUI(self):self.textEdit = QTextEdit()self.setCentralWidget(self.textEdit)self.statusBar()openFile = QAction(QIcon('open.png'), 'Open', self)openFile.setShortcut('Ctrl+O')openFile.setStatusTip('Open new File')openFile.triggered.connect(self.showDialog)menubar = self.menuBar()fileMenu = menubar.addMenu('&File')fileMenu.addAction(openFile)self.setGeometry(300, 300, 550, 450)self.setWindowTitle('File dialog')self.show()def showDialog(self):home_dir = str(Path.home())fname = QFileDialog.getOpenFileName(self, 'Open file', home_dir)if fname[0]:f = open(fname[0], 'r')with f:data = f.read()self.textEdit.setText(data)def main():app = QApplication(sys.argv)ex = Example()sys.exit(app.exec_())if __name__ == '__main__':main()

該示例顯示了一個菜單欄、集中設置的文本編輯小部件和一個狀態欄。菜單項顯示用于選擇文件的 QFileDialog。文件的內容被加載到文本編輯小部件中。

class Example(QMainWindow):def __init__(self):super().__init__()self.initUI()

該示例基于 QMainWindow 小部件,因為我們集中設置了文本編輯小部件。

home_dir = str(Path.home()) fname = QFileDialog.getOpenFileName(self, 'Open file', home_dir)

我們彈出 QFileDialog。 getOpenFileName 方法中的第一個字符串是標題。第二個字符串指定對話框工作目錄。我們使用路徑模塊來確定用戶的主目錄。默認情況下,文件過濾器設置為所有文件 (*)。

if fname[0]:f = open(fname[0], 'r')with f:data = f.read()self.textEdit.setText(data)

讀取選定的文件名并將文件的內容設置為文本編輯小部件。

總結

以上是生活随笔為你收集整理的pyqt5教程8:对话框的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。