Python?PyQt5中窗口數(shù)據(jù)傳遞的示例詳解
開發(fā)應(yīng)用程序時,若只有一個窗口則只需關(guān)心這個窗口里面的各控件之間如何傳遞數(shù)據(jù)。如果程序有多個窗口,就要關(guān)心不同的窗口之間是如何傳遞數(shù)據(jù)。
單一窗口數(shù)據(jù)傳遞
對于單一窗口的程序來說,一個控件的變化會影響另一個控件的變化通過信號與槽的機制就可簡單解決。
import sys from PyQt5.QtWidgets import QWidget, QLCDNumber, QSlider, QVBoxLayout, QApplication from PyQt5.QtCore import Qt class WinForm(QWidget): def __init__(self, parent=None): super(WinForm, self).__init__(parent) self.initUI() def initUI(self): # 先創(chuàng)建滑塊和LCD控件 lcd = QLCDNumber(self) slider = QSlider(Qt.Horizontal, self) vBox = QVBoxLayout() vBox.addWidget(lcd) vBox.addWidget(slider) self.setLayout(vBox) # valueChanged()是QSlider的一個信號函數(shù),只要slider的值發(fā)生改變,就會發(fā)射一個信號 # 然后通過connect連接信號的接收控件lcd slider.valueChanged.connect(lcd.display) # lcd.setBinMode() # lcd.setHexMode() # lcd.setDecMode() # lcd.setOctMode() # lcd.setNumDigits(2) # lcd.setDigitCount(4) # lcd.setSegmentStyle(QLCDNumber.Filled) self.setGeometry(300, 300, 350, 150) self.setWindowTitle("信號與槽:連接滑塊LCD") if __name__ == "__main__": app = QApplication(sys.argv) win = WinForm() win.show() sys.exit(app.exec_())
多窗口數(shù)據(jù)傳遞:調(diào)用屬性
DateDialog.py
from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * class DateDialog(QDialog): def __init__(self, parent=None): super(DateDialog, self).__init__(parent) self.setWindowTitle("DataDialog") # 在布局中添加控件 layout = QVBoxLayout(self) self.datetime = QDateTimeEdit(self) self.datetime.setCalendarPopup(True) self.datetime.setDateTime(QDateTime().currentDateTime()) layout.addWidget(self.datetime) buttons = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel, Qt.Horizontal, self) buttons.accepted.connect(self.accept) buttons.rejected.connect(self.reject) layout.addWidget(buttons) # 從對話框獲取當前日期時間 def dateTime(self): return self.datetime.dateTime() # 使用靜態(tài)函數(shù)創(chuàng)建對話框并返回(date, time, accepted) @staticmethod def getDateTime(parent=None): dialog = DateDialog(parent) result = dialog.exec_() date = dialog.dateTime() return (date.date(), date.time(), result == QDialog.Accepted)
CallDialogMainWin.py
import sys from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * from DateDialog import DateDialog class WinForm(QWidget): def __init__(self, parent=None): super(WinForm, self).__init__(parent) self.resize(400, 90) self.setWindowTitle("對話框關(guān)閉時返回值給主窗口") self.lineEdit = QLineEdit(self) self.button1 = QPushButton("彈出對話框1") self.button1.clicked.connect(self.onButton1Click) self.button2 = QPushButton("彈出對話框2") self.button2.clicked.connect(self.onButton2Click) gridLayout = QGridLayout() gridLayout.addWidget(self.lineEdit) gridLayout.addWidget(self.button1) gridLayout.addWidget(self.button2) self.setLayout(gridLayout) def onButton1Click(self): dialog = DateDialog(self) result = dialog.exec_() date = dialog.dateTime() self.lineEdit.setText(date.date().toString()) print('\n日期對話框的返回值') print('date=%s' % str(date.date())) print('time=%s' % str(date.time())) print('result=%s' % result) dialog.destroy() def onButton2Click(self): date, time, result = DateDialog.getDateTime() self.lineEdit.setText(date.toString()) print('\n日期對話框返回值') print('date=%s' % str(date)) print('time=%s' % str(time)) print('result=%s' % str(result)) if __name__ == "__main__": app = QApplication(sys.argv) win = WinForm() win.show() sys.exit(app.exec_())
多窗口數(shù)據(jù)傳遞:信號與槽
子窗口發(fā)射信號,父窗口接收信號。
DateDialog2.py
from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * class DateDialog(QDialog): Signal_OneParameter = pyqtSignal(str) def __init__(self, parent=None): super(DateDialog, self).__init__(parent) self.setWindowTitle("子窗口:用來發(fā)射信號") # 在布局中添加控件 layout = QVBoxLayout(self) self.label = QLabel(self) self.label.setText("前者發(fā)射內(nèi)置信號\n后者發(fā)射自定義信號") self.datetime_inner = QDateTimeEdit(self) self.datetime_inner.setCalendarPopup(True) self.datetime_inner.setDateTime(QDateTime.currentDateTime()) self.datetime_eimt = QDateTimeEdit(self) self.datetime_eimt.setCalendarPopup(True) self.datetime_eimt.setDateTime(QDateTime.currentDateTime()) layout.addWidget(self.label) layout.addWidget(self.datetime_inner) layout.addWidget(self.datetime_eimt) # 使用兩個button(Ok和Cancel)分別連接accept()和reject()槽函數(shù) buttons = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel, Qt.Horizontal, self) buttons.accepted.connect(self.accept) buttons.rejected.connect(self.reject) layout.addWidget(buttons) self.datetime_eimt.dateTimeChanged.connect(self.emit_signal) def emit_signal(self): date_str = self.datetime_eimt.dateTime().toString() self.Signal_OneParameter.emit(date_str)
CallDialogMainWin2.py
import sys from PyQt5.QtCore import * from PyQt5.QtWidgets import * from PyQt5.QtGui import * from DateDialog2 import DateDialog class WinForm (QWidget): def __init__(self, parent=None): super(WinForm, self).__init__(parent) self.resize(400, 90) self.setWindowTitle("信號與槽傳遞參數(shù)的示例") self.open_btn = QPushButton("獲取時間") self.lineEdit_inner = QLineEdit(self) self.lineEdit_emit = QLineEdit(self) self.open_btn.clicked.connect(self.openDialog) self.lineEdit_inner.setText("接收子窗口內(nèi)置信號的時間") self.lineEdit_emit.setText("接收子窗口自定義信號的時間") grid = QGridLayout() grid.addWidget(self.lineEdit_inner) grid.addWidget(self.lineEdit_emit) grid.addWidget(self.open_btn) self.setLayout(grid) def openDialog(self): dialog = DateDialog(self) '''連接子窗口的內(nèi)置信號與主窗口的槽函數(shù)''' dialog.datetime_inner.dateTimeChanged.connect(self.deal_inner_slot) '''連接子窗口的自定義信號與主窗口的槽函數(shù)''' dialog.Signal_OneParameter.connect(self.deal_emit_slot) dialog.show() def deal_inner_slot(self, date): print(date) self.lineEdit_inner.setText(date.toString()) def deal_emit_slot(self, dateStr): self.lineEdit_emit.setText(dateStr) if __name__ == "__main__": app = QApplication(sys.argv) form = WinForm() form.show() sys.exit(app.exec_())
到此這篇關(guān)于Python PyQt5中窗口數(shù)據(jù)傳遞的示例詳解的文章就介紹到這了,更多相關(guān)PyQt5窗口數(shù)據(jù)傳遞內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
用Pytorch訓練CNN(數(shù)據(jù)集MNIST,使用GPU的方法)
今天小編就為大家分享一篇用Pytorch訓練CNN(數(shù)據(jù)集MNIST,使用GPU的方法),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08python中關(guān)于requests里的timeout()用法
這篇文章主要介紹了python中關(guān)于requests里的timeout()用法,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08python GUI庫圖形界面開發(fā)之PyQt5滑塊條控件QSlider詳細使用方法與實例
這篇文章主要介紹了python GUI庫圖形界面開發(fā)之PyQt5滑塊條控件QSlider詳細使用方法與實例,需要的朋友可以參考下2020-02-02Python報錯ImportError:?IProgress?not?found.?Please?update
在使用Jupyter Notebook或JupyterLab進行交互式編程時,我們可能會遇到各種導(dǎo)入錯誤,本文就來介紹一下Python報錯ImportError:?IProgress?not?found.?Please?update?jupyter?and?ipywidgets解決,感興趣的可以了解一下2024-06-06