世界动态:气轻PyQt5 16动态添加删除控件

2023-03-21 06:51:15       来源:哔哩哔哩

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

from PyQt5 import QtCore, QtGui


(相关资料图)

from PyQt5.QtWidgets import *

from PyQt5.QtGui import QFont, QIcon, QPixmap

from PyQt5.QtCore import Qt, QDate, QSize

import sys

import datetime

class PyQt516(QWidget):

def __init__(self):

super().__init__()

self.cal = None

self.initUI()

def initUI(self):

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

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

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.AlignCenter)          # 居中设置

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)               # 设置位置和大小

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 = PyQt516()

sys.exit(app.exec_())

执行结果

参照【气轻PyQt6 16动态添加删除控件

关键词:
x 广告
x 广告

Copyright @  2015-2022 海外生活网版权所有  备案号: 沪ICP备2020036824号-21   联系邮箱:562 66 29@qq.com