氣輕PyQt6 13 不同窗口間的槽機(jī)制
?
點(diǎn)擊按鈕后在新窗口中顯示日歷控件,在日歷控件中選擇日期后,新窗口關(guān)閉。通過槽機(jī)制將選擇的日期顯示在主窗口中的文本框里。
?
from PyQt6.QtWidgets import *
from PyQt6.QtGui import QFont, QIcon, QPixmap
from PyQt6.QtCore import Qt, QDate, pyqtSignal
import sys
import datetime
?
class PyQt613(QWidget):
??? def __init__(self):
??????? super().__init__()
??????? self.initUI()
??? def initUI(self):
??????? self.setWindowTitle('氣輕PyQt6')??????????????? # 設(shè)置窗口標(biāo)題
??????? self.resize(300, 60)??????????????????????????? # 設(shè)置窗口大小
?
??????? dateData = datetime.datetime.now().date()
??????? dateLabel = dateData.strftime('%Y-%m-%d')
??????? self.edit = QLineEdit(self)???????????????????? # 輸入框
??????? self.edit.setGeometry(10, 0,230, 40)??????? ????# 設(shè)置位置和大小
??????? self.edit.setReadOnly(True)???????????????????? # 只讀
??????? self.edit.setText(dateLabel)
??????? self.edit.setAlignment(Qt.AlignmentFlag.AlignHCenter)# 居中設(shè)置
??????? self.edit.setStyleSheet('background-color:#FFE4B5;color : #32CD32; \
??????????????????? font: bold italic large /"Times New Roman/";font-size:25px')
?
??????? self.btn = QPushButton('', self)
??????? self.btn.setGeometry(250,0,40,40)?????????????? # 設(shè)置位置和大小
??????? self.btn.setIcon(QIcon(QPixmap('Date.png')))
??????? self.btn.clicked.connect(self.calendarOn)
?
??????? 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)??? # 連接信號(hào)與槽
?
class CalendarWin(QWidget):
??? signal = pyqtSignal( str )????????????????????????? #定義一個(gè)信號(hào)
??? 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)??? # 按下按鈕時(shí)發(fā)送信號(hào)
?
??? def emitDate(self,date):
?? ?????self.signal.emit(date.toString("yyyy-MM-dd"))
??????? self.close()
?
if __name__ == '__main__':
??? app = QApplication(sys.argv)
??? window = PyQt613()
??? sys.exit(app.exec())
?
執(zhí)行結(jié)果
