气轻PyQt5 25 二级菜单
2023-05-11 08:03:41 哔哩哔哩

import sys


(相关资料图)

from PyQt5.QtWidgets import *

from PyQt5.QtGui import QIcon, QPixmap

from PyQt5.QtCore import Qt, QDate, QSize

import datetime

class PyQt525(QMainWindow):

def __init__(self):

super().__init__()

self.cal = None

self.initUI()

def initUI(self):

self.setGeometry(300, 300, 320, 290)

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

self.setStyleSheet('background-color:#7AC5CD')

quit = QAction('&退出', self)

quit.setShortcut('Ctrl+Q')

quit.triggered.connect(QApplication.instance().quit)

cal = QAction('&选择日期', self)

cal.setShortcut('Ctrl+C')

cal.triggered.connect(self.calendarOn)

self.statusBar()

menubar = self.menuBar()

menubar.setStyleSheet('background-color:#98F5FF;color : #32CD32; \

font: large /"SimSun/";font-size:20px')

fileMenu = menubar.addMenu('&File')

fileMenu.addAction(cal)

fileMenu.addAction(quit)

opt1 = QAction("早饭前", self)

opt1.triggered.connect(self.updateLabel)

opt2 = QAction("晚饭后", self)

opt2.triggered.connect(self.updateLabel)

edit = menubar.addMenu("Edit")

subMenu = edit.addMenu("选择时段")

subMenu.addAction(opt1)

subMenu.addAction(opt2)

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

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

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

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

self.edit.setText(dateLabel)

self.edit.setReadOnly(True)                     # 只读

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

self.edit.setStyleSheet('background-color:#2F4F4F;color : #32CD32; \

font: large /"SimSun/";font-size:25px')

self.btn = QPushButton('', self)

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

self.btn.setIcon(QIcon(QPixmap('F:/Data/Image/icon/Date.png')))

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

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

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

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

self.tag.setGeometry(230, 40,80, 40)            # 设置位置和大小

self.tag.setText('早饭前')

self.tag.setStyleSheet('background-color:#2F4F4F;color : #32CD32; \

font: large /"SimSun/";font-size:25px')

self.show()

def updateLabel(self, a):

self.tag.setText(self.sender().text())

def calendarOn( self ):

if self.cal != None:

return

self.cal = QCalendarWidget(self)

self.cal.setGridVisible(True)

self.cal.setGeometry(10, 120,270, 140)          # 设置位置和大小

self.cal.clicked[QDate].connect(self.updateDate)

self.cal.show()                                 # 显示日历控件

def updateDate(self,date):

self.edit.setText(date.toString("yyyy-MM-dd"))

self.cal.deleteLater()                          # 删除日历控件

self.cal = None

if __name__ == '__main__':

app = QApplication(sys.argv)

window = PyQt525()                                  # 创建窗口

sys.exit(app.exec_())

执行结果

相关新闻: