氣輕PyQt6 14 單選按鈕(QRadioButton), 按鈕組
?
增加了六個單選按鈕,形成了一個按鈕組。點擊單選按鈕后,選擇的結果會顯示在標簽控件。
?
from PyQt6.QtWidgets import *
from PyQt6.QtGui import QFont, QIcon, QPixmap
from PyQt6.QtCore import Qt, QDate, pyqtSignal
import sys
import datetime
import numpy as np
?
timeLabel = ['早飯前', '早飯后', '午飯前', '午飯后', '晚飯前', '晚飯后']
?
class PyQt614(QWidget):
??? def __init__(self):
??????? super().__init__()
??????? self.initUI()
??? def initUI(self):
??????? self.setWindowTitle('氣輕PyQt6')??????????????? # 設置窗口標題
??????? self.resize(420, 120)?????????????????????????? # 設置窗口大小
?
??????? 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:#FFE4B5;color : #32CD32; \
??????????????????? font: bold italic large /"Times New Roman/";font-size:25px')
?
??????? self.btn = QPushButton('', self)
??????? self.btn.setGeometry(250,0,40,40)??????????? ???# 設置位置和大小
??????? self.btn.setIcon(QIcon(QPixmap('Date.png')))
??????? self.btn.clicked.connect(self.calendarOn)
?
??????? 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.AlignmentFlag.AlignHCenter)# 居中設置
??????? self.stage.setStyleSheet('background-color:yellow;color : #6495ED; \
??????????????????? font: bold italic large /"Times New Roman/";font-size:25px')
?
??????? self.rg = QButtonGroup(self)
???????
??????? rdict = dict.fromkeys(timeLabel)
??????? x, y=np.meshgrid([80, 180, 280], [60, 80])
??????? 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)
?
??????? 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 updateStage(self,val):
??????? self.stage.setText(timeLabel[self.rg.checkedId()])
?
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)??? # 按下按鈕時發(fā)送信號
?
??? def emitDate(self,date):
??????? self.signal.emit(date.toString("yyyy-MM-dd"))
??????? self.close()
?
if __name__ == '__main__':
??? app = QApplication(sys.argv)
??? window = PyQt614()
??? sys.exit(app.exec())
?
執(zhí)行結果
