当前位置: 首页 > 滚动 > > 内容页

气轻PyQt5 15 不同窗口控件的信息传递

发布时间:2023-03-17 08:03:09 来源:哔哩哔哩

【气轻PyQt5 13】用槽函数实现了不同窗口控件间的信息传递。这里用另外一种方式来实现不同窗口控件间的信息传递。点击单选按钮后,将六个单选按钮组成的按钮组显示在新窗口中,点击按钮后,窗口会关闭,选择的结果会显示在主窗口的标签控件。


(资料图片仅供参考)

from PyQt5.QtWidgets import *

from PyQt5.QtGui import QFont, QIcon, QPixmap

from PyQt5.QtCore import Qt, QDate, pyqtSignal, QSize

import sys

import datetime

import numpy as np

timeLabel = ['早饭前', '早饭后', '午饭前', '午饭后', '晚饭前', '晚饭后']

class PyQt515(QWidget):

def __init__(self):

super().__init__()

self.initUI()

def initUI(self):

self.setWindowTitle('气轻PyQt5')                # 设置窗口标题

self.resize(450, 50)                            # 设置窗口大小

self.setStyleSheet('background-color:#40E0D0')

dateData = datetime.datetime.now().date()

dateLabel = dateData.strftime('%Y-%m-%d')

self.edit = QLineEdit(self)                     # 输入框

self.edit.setGeometry(10, 0,230, 40)            # 设置位置和大小

self.edit.setReadOnly(True)                     # 只读

self.edit.setText(dateLabel)

self.edit.setAlignment(Qt.AlignCenter)          # 居中设置

self.edit.setStyleSheet('background-color:#6495ED;color : #32CD32; \

font: bold large /"Times New Roman/";font-size:25px')

self.btn = QPushButton('', self)

self.btn.setGeometry(240,0,40,40)               # 设置位置和大小

self.btn.setIcon(QIcon(QPixmap('Date.png')))

self.btn.setIconSize(QSize(36,36))

self.btn.clicked.connect(self.calendarOn)

self.btn.setStyleSheet('background-color:#CDC5BF')

self.stage = QLabel(self)                       #设置label信息

self.stage.setGeometry(290, 0,120, 40)          # 设置位置和大小

self.stage.setText(timeLabel[0])

self.stage.setObjectName('label')

self.stage.setToolTip('时段')                   # 设置label提示

self.stage.setAlignment(Qt.AlignCenter)         # 居中设置

self.stage.setStyleSheet('background-color:#6495ED;color : #32CD32; \

font: bold large /"Times New Roman/";font-size:25px')

self.bTime = QPushButton('', self)

self.bTime.resize(40,40)

self.bTime.move(400,0)

self.bTime.setFont(QFont('Arial',20))

self.bTime.setIcon(QIcon(QPixmap('D:/CHEN/Python/GLXY/sys/DataEntry.png')))

self.bTime.setIconSize(QSize(36,36))

self.bTime.clicked.connect(self.StageOn)

self.bTime.setStyleSheet('background-color:#CDC5BF')

self.show()

def updateEdit(self,t):

self.edit.setText(t)

def calendarOn( self ):

self.calWin = CalendarWin(self.edit)

self.calWin.show()

self.calWin.move(900,700)

self.calWin.signal.connect(self.updateEdit)    # 连接信号与槽

def StageOn( self ):

self.stageWin = StageWin(self.stage)

self.stageWin.show( )

self.stageWin.move(740,560)

class StageWin(QWidget):

def __init__(self,t):

super().__init__()

self.t = t

self.setWindowTitle('时段')

self.resize(440, 60)

self.setStyleSheet('background-color:#C1CDCD')

self.rg = QButtonGroup(self)

rdict = dict.fromkeys(timeLabel)

x, y=np.meshgrid([80, 180, 280], [10, 30])

xdict = dict(zip(timeLabel, x.ravel()))

ydict = dict(zip(timeLabel, y.ravel()))

for i, s in enumerate(timeLabel):

rdict[s] = QRadioButton(self)

rdict[s].setText(s)

rdict[s].move(xdict[s], ydict[s])

self.rg.addButton(rdict[s],i)

rdict[timeLabel[0]].setChecked(True)            # 设置第一个单选框被选中

self.rg.buttonClicked.connect(self.updateStage)

def updateStage(self,val):

self.t.setText(timeLabel[self.rg.checkedId()])

self.close()

class CalendarWin(QWidget):

signal = pyqtSignal( str )                          #定义一个信号

def __init__(self,t):

super().__init__()

self.t = t

self.setWindowTitle('日期')

self.resize(440, 220)

cal = QCalendarWidget(self)

cal.setGridVisible(True)

cal.resize(400,180)

cal.move(10,10)

cal.clicked[QDate].connect(self.emitDate)    # 按下按钮时发送信号

def emitDate(self,date):

self.signal.emit(date.toString("yyyy-MM-dd"))

self.close()

if __name__ == '__main__':

app = QApplication(sys.argv)

window = PyQt515()

sys.exit(app.exec_())

执行结果

参照【气轻PyQt6 15

推荐阅读