气轻PyQt6 16动态添加删除控件_世界微动态

点击按钮后,会在界面内添加日历控件。选择日期后,将选择结果传递给文本框后,日历控件将被删除。


【资料图】

from PyQt6 import QtCore, QtGui

from PyQt6.QtWidgets import *

from PyQt6.QtGui import QPalette, QFont, QColor, QIcon, QPixmap

from PyQt6.QtCore import Qt, QDate, QSize

import sys

import datetime

class PyQt616(QWidget):

def __init__(self):

super().__init__()

self.cal = None

self.initUI()

def initUI(self):

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

self.resize(290, 220)                           # 设置窗口大小

self.setStyleSheet('background-color:#8470FF')

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

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

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

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

self.edit.setText(dateLabel)

self.edit.setReadOnly(True)                     # 只读

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

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

font: bold italic large /"SimSun/";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.show()

def calendarOn( self ):

if self.cal != None:

return

self.cal = QCalendarWidget(self)

self.cal.setGridVisible(True)

self.cal.setGeometry(10, 60,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 = PyQt616()

sys.exit(app.exec())

执行结果

标签:

X 广告
X 广告

Copyright ©  2015-2022 华尔街舞蹈网版权所有  备案号:沪ICP备2022005074号-44   联系邮箱:58 55 97 3@qq.com