Getting started with PyQt5 (ten) drag & drop & clipboard & calendar & date and time

table of Contents

1. Let the control support drag and drop actions

2. Use the clipboard

3. Calendar control

Four. Set different styles of date and time

 5. Advanced operation of date and time control


1. Let the control support drag and drop actions

Let the control support drag and drop actions

Process:
A.setDrapEnabled(True) Set A to support drag and drop
B. setAcceptDrops(True) Set B to receive


B needs two events:
1. dragEnterEvent is triggered by dragging A to B
2. dropEvent is triggered when A is dropped in the area of ​​B

 

Code:

import sys
from PyQt5.QtWidgets import *


#下拉框类
class MyComboBox(QComboBox):
    def __init__(self):
        super(MyComboBox, self).__init__()
        #设置下拉框可接收
        self.setAcceptDrops(True)

    #别的控件拖进来以后,还没松鼠标时,下面的函数触发
    def dragEnterEvent(self,e):
        print(e)
        #是否是文本
        if e.mimeData().hasText():
            e.accept()
        else:
            e.ignore()

    #当控件拖进来放下时,下面的函数触发
    def dropEvent(self,e):
        #此时self代表当前下拉列表控件,
        self.addItem(e.mimeData().text())


class DragDropDemo(QWidget):
    def __init__(self):
        super(DragDropDemo, self).__init__()
        formLayout=QFormLayout()
        formLayout.addRow(QLabel('请将左边的文本拖到右边的下拉列表中'))
        lineEdit=QLineEdit()
        #让QLinEdit控件可拖动
        lineEdit.setDragEnabled(True)
        #第一个类MyComboBox的实例
        combo=MyComboBox()
        formLayout.addRow(lineEdit,combo)

        self.setLayout(formLayout)
        self.setWindowTitle('拖拽案例')
        self.resize(400,100)


if __name__=='__main__':
    app=QApplication(sys.argv)
    main=DragDropDemo()
    main.show()
    sys.exit(app.exec_())

operation result:

Enter in the text input box on the left, and then select the part to be dragged, hold down and drag it to the drop-down box on the right, and the drop-down box will add the part you just dragged in the past. Try it yourself.

    

 

2. Use the clipboard

Code:

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *


class ClipBoardDemo(QDialog):
    def __init__(self):
        super(ClipBoardDemo, self).__init__()
        #控件

        #定义六个复制粘贴按钮,用来实现复制粘贴文本,图像和HTML网页
        textCopyBtn=QPushButton('复制文本')
        textPasteBtn=QPushButton('粘贴文本')
        htmlCopyBtn=QPushButton('复制HTML')
        htmlPasteBtn=QPushButton('粘贴HTML')
        imageCopyBtn=QPushButton('复制图像')
        imagePasteBtn = QPushButton('粘贴图像')

        #多行文本
        self.textLabel=QLabel('默认文本')

        #标签用于展示图片
        self.imageLabel=QLabel()
        #self.imageLabel.setPixmap(QPixmap('../picture/LinuxLogo.jpg'))

        #定义网格栅格布局
        layout=QGridLayout()
        layout.addWidget(textCopyBtn,0,0)
        layout.addWidget(imageCopyBtn,0,1)
        layout.addWidget(htmlCopyBtn,0,2)
        layout.addWidget(textPasteBtn, 1, 0)
        layout.addWidget(imagePasteBtn, 1, 1)
        layout.addWidget(htmlPasteBtn, 1, 2)
        layout.addWidget(self.textLabel,2,0,1,2)#坐标,行占的单位宽度,列占的单位宽度
        layout.addWidget(self.imageLabel,2,2)

        self.setLayout(layout)

        #信号与槽
        textCopyBtn.clicked.connect(self.copyText)
        textPasteBtn.clicked.connect(self.pasteText)
        htmlCopyBtn.clicked.connect(self.copyHtml)
        htmlPasteBtn.clicked.connect(self.pasteHtml)
        imageCopyBtn.clicked.connect(self.copyImage)
        imagePasteBtn.clicked.connect(self.pasteImage)

        self.setWindowTitle('剪贴板演示')

    #槽函数
    #bug解决: QApplication没有括号()
    def copyText(self):
        #剪贴板对象
        clipboard=QApplication.clipboard()
        clipboard.setText('hello world')

    def pasteText(self):
        clipboard=QApplication.clipboard()
        self.textLabel.setText(clipboard.text())

    def copyImage(self):
        clipboard=QApplication.clipboard()
        clipboard.setPixmap(QPixmap('../picture/LinuxLogo.jpg'))

    #自己从文件夹里复制个图片就粘贴不了??????????????
    def pasteImage(self):
        clipboard=QApplication.clipboard()
        #clipboard.pixmap()是从剪贴板获得图像
        self.imageLabel.setPixmap(clipboard.pixmap())

    def copyHtml(self):
        mimeData=QMimeData() #获得数据类型???
        mimeData.setHtml('<b>Bold and <font color=red>Red</font></b>')
        clipboard=QApplication.clipboard()
        clipboard.setMimeData(mimeData)

    def  pasteHtml(self):
        clipboard=QApplication.clipboard()
        mimeData=clipboard.mimeData() #获得剪贴板数据

        #如果剪贴板数据是html   但是这里普通文本也可以。。。??????
        if mimeData.hasHtml():
            self.textLabel.setText(mimeData.html())


if __name__=='__main__':
    app=QApplication(sys.argv)
    main=ClipBoardDemo()
    main.show()
    sys.exit(app.exec_())

operation result:

Try it yourself, the clipboard is the clipboard on your computer, and test it according to the implemented functions.

There is a bug in the code under Windows, which has been commented in the code.

 

3. Calendar control

Calendar control:

QCalendarWidget

Code:

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import QDate


class MyCalendar(QDialog):
    def __init__(self):
        super(MyCalendar, self).__init__()
        self.initUI()

    def initUI(self):
        #日历
        self.cal=QCalendarWidget(self)
        #设置允许显示的最小日期
        self.cal.setMinimumDate(QDate(1988,1,1))
        # 设置允许显示的最大日期
        self.cal.setMaximumDate(QDate(2088,1,1))
        #以网格形式显示
        self.cal.setGridVisible(True)
        #移动位置
        self.cal.move(20,20)

        #标签
        self.label=QLabel(self)
        #获取当前日期
        date=self.cal.selectedDate()
        #格式化
        self.label.setText(date.toString('yyyy-MM-dd dddd'))# mm不行
        self.label.move(20,300)

        #信号与槽
        self.cal.clicked.connect(self.showDate)

        self.setWindowTitle('日历演示')
        self.resize(400,350)

    def showDate(self,date):
        #self.label.setText(date.toString('yyyy-MM-dd dddd'))
        self.label.setText(self.cal.selectedDate().toString('yyyy-MM-dd dddd'))


if __name__=='__main__':
    app=QApplication(sys.argv)
    main=MyCalendar()
    main.show()
    sys.exit(app.exec_())

operation result:

 

Four. Set different styles of date and time

Controls:

QDateTimeEdit

Code:

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *


class DateTimeEdit1(QWidget):
    def __init__(self):
        super(DateTimeEdit1, self).__init__()
        self.initUI()

    def initUI(self):
        #垂直布局
        vlayout=QVBoxLayout()
        dateTimeEdit1=QDateTimeEdit()
        dateTimeEdit2 = QDateTimeEdit(QDateTime.currentDateTimeUtc())#传入当前时间

        dateEdit=QDateTimeEdit(QDate.currentDate())#传入当前日期
        timeEdit=QDateTimeEdit(QTime.currentTime())#传入当前日期

        dateTimeEdit1.setDisplayFormat('yyyy-MM-dd HH:mm:ss')
        dateTimeEdit2.setDisplayFormat('yyyy/MM/dd HH:mm:ss')

        dateEdit.setDisplayFormat('yyyy.MM.dd')
        timeEdit.setDisplayFormat('HH:mm:ss')

        vlayout.addWidget(dateTimeEdit1)
        vlayout.addWidget(dateTimeEdit2)
        vlayout.addWidget(dateEdit)
        vlayout.addWidget(timeEdit)

        self.setLayout(vlayout)
        self.resize(300,90)
        self.setWindowTitle('设置不同风格的日期和时间')


if __name__=='__main__':
    app=QApplication(sys.argv)
    main=DateTimeEdit1()
    main.show()
    sys.exit(app.exec_())

operation result:

 

 5. Advanced operation of date and time control

 

method description
setDisplayFormat Set the time format of the date
  yyyy: represents the year, expressed by 4 as a number
  MM: represents the month, the value range is 01-12
  dd: represents the day, the value range is 01-31
  HH: represents the hour, the value range is 00-23
  mm: represents the minute, the value range is 00-59
  ss: represents the second, the value range is 00-59
setMinimumDate () Set the minimum date of the control
setMaximumDate() Set the maximum date of the control
time() Back to edit time
date() Return the date of the edit

Code:

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *


class DateTimeEdit1(QWidget):
    def __init__(self):
        super(DateTimeEdit1, self).__init__()
        self.initUI()

    def initUI(self):
        #垂直布局
        vlayout=QVBoxLayout()

        dateTimeEdit1=QDateTimeEdit()
        #设置最小时间是当前时间往前倒推365天
        dateTimeEdit1.setMinimumDate(QDate.currentDate().addDays(-365))
        # 设置最大时间是当后时间往后推365天
        dateTimeEdit1.setMaximumDate(QDate.currentDate().addDays(365))#bug: 第一个控件只能往上调一年,但不能往下调一年 ???
        self.dateTimeEdit=dateTimeEdit1

        dateTimeEdit2 = QDateTimeEdit(QDateTime.currentDateTimeUtc())#传入当前时间
        #下拉是日历
        dateTimeEdit2.setCalendarPopup(True)

        dateEdit=QDateTimeEdit(QDate.currentDate())#传入当前日期
        timeEdit=QDateTimeEdit(QTime.currentTime())#传入当前日期

        dateTimeEdit1.setDisplayFormat('yyyy-MM-dd HH:mm:ss')
        dateTimeEdit2.setDisplayFormat('yyyy/MM/dd HH:mm:ss')

        dateEdit.setDisplayFormat('yyyy.MM.dd')
        timeEdit.setDisplayFormat('HH:mm:ss')

        dateTimeEdit1.dateChanged.connect(self.onDateChange)
        dateTimeEdit1.timeChanged.connect(self.onTimeChanged)
        dateTimeEdit1.dateTimeChanged.connect(self.onDateTimeChanged)

        self.btn=QPushButton('获取日期和时间')
        self.btn.clicked.connect(self.onButtonClick)

        vlayout.addWidget(dateTimeEdit1)
        vlayout.addWidget(dateTimeEdit2)
        vlayout.addWidget(dateEdit)
        vlayout.addWidget(timeEdit)
        vlayout.addWidget(self.btn)

        self.setLayout(vlayout)
        self.resize(300,90)
        self.setWindowTitle('设置不同风格的日期和时间')

    #日期变化(日期变化这个才触发)
    def onDateChange(self,date):
        print(date)

    #时间变化(时间变化这个才触发)
    def onTimeChanged(self,time):
        print(time)

    #日期和时间变化(日期或者时间变化这个都会触发)
    def onDateTimeChanged(self,datetime):
        print(datetime)

    def onButtonClick(self):
        #获得日期和时间
        datetime=self.dateTimeEdit.dateTime()
        print('当前设置日期:',datetime)
        #最大日期
        print('最大日期:',self.dateTimeEdit.maximumDate())
        #最大日期和时间
        print('最大日期和时间:',self.dateTimeEdit.maximumDateTime())
        # 最小日期
        print('最小日期:',self.dateTimeEdit.minimumDate())
        # 最小日期和时间
        print('最小日期和时间:',self.dateTimeEdit.minimumDateTime())


if __name__=='__main__':
    app=QApplication(sys.argv)
    main=DateTimeEdit1()
    main.show()
    sys.exit(app.exec_())

operation result:

    

Try it yourself, there are bugs in the code, which have been annotated and have not been solved yet. .

 

 

 

 

 

Guess you like

Origin blog.csdn.net/weixin_44593822/article/details/112892204