氣輕PyQt517 復(fù)選按鈕(QCheckBox)
?
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QFont, QIcon, QPixmap
from PyQt5.QtCore import Qt, QDate, pyqtSignal
import sys
import datetime
import numpy as np
?
timeLabel = ['早飯前', '早飯后', '午飯前', '午飯后', '晚飯前', '晚飯后']
?
class PyQt517(QWidget):
??? def __init__(self):
??????? super().__init__()
??????? self.initUI()
??? def initUI(self):
??????? self.setWindowTitle('氣輕PyQt5')??????????????? # 設(shè)置窗口標(biāo)題
???? ???self.resize(610, 50)??????????????????????????? # 設(shè)置窗口大小
??????? 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)??????????? # 設(shè)置位置和大小
??????? self.edit.setReadOnly(True)???????????????????? # 只讀
??????? self.edit.setText(dateLabel)
??????? self.edit.setAlignment(Qt.AlignCenter)????????? # 居中設(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(240,0,40,40)?????????????? # 設(shè)置位置和大小
??????? self.btn.setIcon(QIcon(QPixmap('Date.png')))
??????? self.btn.clicked.connect(self.calendarOn)
??????? self.btn.setStyleSheet('background-color:#DAA520')
??????? self.btn.setToolTip('選擇日期')???????????????? # 設(shè)置label提示
?
??????? self.stage = QLabel(self)?????????????????????? #設(shè)置label信息
??????? self.stage.setGeometry(280, 0,120, 40)????????? # 設(shè)置位置和大小
??????? self.stage.setText(timeLabel[0])
??????? self.stage.setAlignment(Qt.AlignCenter)???????? # 居中設(shè)置
??????? self.stage.setStyleSheet('background-color:yellow;color : #6495ED; \
??????????????????? font: bold italic 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.clicked.connect(self.StageOn)
??????? self.bTime.setToolTip('選擇時(shí)段')?????????????? # 設(shè)置label提示
??????? self.bTime.setStyleSheet('background-color:#DAA520')
?
??????? self.drug = QLabel(self)??????????????????????? #設(shè)置label信息
??????? self.drug.setGeometry(440, 0,120, 40)?????????? # 設(shè)置位置和大小
??????? self.drug.setText('')
??????? self.drug.setObjectName('label')
??????? self.drug.setAlignment(Qt.AlignmentFlag.AlignHCenter)# 居中設(shè)置
??????? self.drug.setStyleSheet('background-color:#7A67EE;color : #32CD32; \
??????????????????? font: bold large /"Times New Roman/";font-size:25px')
?
??????? self.uptake = QPushButton('', self)
??????? self.uptake.resize(40,40)
??????? self.uptake.move(560,0)
??????? self.uptake.setFont(QFont('Arial',20))
??????? self.uptake.setIcon(QIcon(QPixmap('cachet.png')))
??????? self.uptake.clicked.connect(self.DrugOn)
??????? self.uptake.setToolTip('選擇用藥')????????????? # 設(shè)置標(biāo)簽提示
??????? self.uptake.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.signal.connect(self.updateEdit)??? # 連接信號(hào)與槽
?
??? def StageOn( self ):
??????? self.stageWin = StageWin(self.stage)
??????? self.stageWin.show( )
??????? self.stageWin.move(740,580)
?
??? def DrugOn( self ):
??????? self.stageWin = DrugWin(self.drug)
??????? self.stageWin.show( )
??????? self.stageWin.move(740,580)
?
class DrugWin(QWidget):
??? def __init__(self, t):
??????? super().__init__()
??????? self.t = t
??????? self.setWindowTitle('選擇用藥')
??????? self.resize(440, 60)
??????? self.setStyleSheet('background-color:#F5DEB3')
?
??????? drugLabel = ['甲', '乙', '丙', '丁', '戊', '己']
?
??????? self.drg = QButtonGroup(self)
??????? self.drg.setExclusive(False)
?
??????? cdict = dict.fromkeys(drugLabel)
??????? x, y = np.meshgrid([80, 180, 280], [10, 30])
??????? xdict = dict(zip(drugLabel, x.ravel()))
??????? ydict = dict(zip(drugLabel, y.ravel()))
?
??????? for i, s in enumerate(drugLabel):
??????????? cdict[s] = QCheckBox(self)
??????????? cdict[s].setText(s)
??????????? cdict[s].move(xdict[s], ydict[s])
??????????? self.drg.addButton(cdict[s], i)
??????? self.drg.buttonClicked.connect(lambda: self.updateDrug(cdict))
?
??? def updateDrug(self, bdict):
??????? tmpbuf = []
??????? for c in bdict.values():
??????????? tmpbuf.append(c.isChecked())
??????? wno = np.where(np.array(tmpbuf) == True)
??????? drugLabel = np.array(list(bdict.keys()))
?
??????? self.t.setText('|'.join(drugLabel[list(wno[0])]))
?
class StageWin(QWidget):
??? def __init__(self, t):
??????? super().__init__()
??????? self.t = t
??????? self.setWindowTitle('時(shí)段')
??? ????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)
??????? 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)? # 定義一個(gè)信號(hào)
??? def __init__(self, t):
??????? super().__init__()
??????? self.t = t
??????? self.setWindowTitle('日期')
??????? cal = QCalendarWidget(self)
??????? cal.setGridVisible(True)
??????? self.setGeometry(800,600,280, 220)????????? # 設(shè)置位置和大小
??????? 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 = PyQt517()
??? sys.exit(app.exec_())
?
執(zhí)行結(jié)果
參照【氣輕PyQt617】