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

from PyQt6.QtWidgets import *

from PyQt6.QtGui import QFont, QIcon, QPixmap


(资料图片)

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

import sys

import datetime

import numpy as np

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

class PyQt615(QWidget):

def __init__(self):

super().__init__()

self.initUI()

def initUI(self):

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

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.AlignmentFlag.AlignHCenter)# 居中设置

self.edit.setStyleSheet('background-color:#7A67EE;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:#DAA520')

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

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

self.stage.setText(timeLabel[0])

self.stage.setObjectName('label')

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

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

self.stage.setStyleSheet('background-color:#7A67EE;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('DataEntry.png')))

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

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

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

self.show()

def updateEdit(self,t):

self.edit.setText(t)

def calendarOn( self ):

self.calWin = CalendarWin(self.edit)

self.calWin.show()

self.calWin.move(740,560)

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:#F5DEB3')

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,160)

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 = PyQt615()

sys.exit(app.exec())

执行结果

推荐内容