上傳檔案到「pyqt5/CODE/元件使用範例」

This commit is contained in:
M11212051 2024-05-05 22:29:39 +08:00
parent ce5d9029f6
commit f7d8ad4dd9
5 changed files with 316 additions and 0 deletions

View File

@ -0,0 +1,42 @@
# -*- coding: utf-8 -*-
'''
簡介
PyQt5中QCalendarWidget範例
'''
import sys
from PyQt5 import QtCore
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import QDate
class CalendarExample( QWidget):
def __init__(self):
super(CalendarExample, self).__init__()
self.initUI()
def initUI(self):
self.cal = QCalendarWidget(self)
self.cal.setMinimumDate(QDate(1980, 1, 1))
self.cal.setMaximumDate(QDate(3000, 1, 1))
self.cal.setGridVisible(True)
self.cal.move(20, 20)
self.cal.clicked[QtCore.QDate].connect(self.showDate)
self.lbl = QLabel(self)
date = self.cal.selectedDate()
self.lbl.setText(date.toString("yyyy-MM-dd dddd"))
self.lbl.move(20, 300)
self.setGeometry(100,100,400,350)
self.setWindowTitle('Calendar範例')
def showDate(self, date):
self.lbl.setText(date.toString("yyyy-MM-dd dddd") )
if __name__ == '__main__':
app = QApplication(sys.argv)
demo = CalendarExample()
demo.show()
sys.exit(app.exec_())

View File

@ -0,0 +1,80 @@
# -*- coding: utf-8 -*-
'''
簡介
PyQt5中QClipboard範例
'''
import os
import sys
from PyQt5.QtCore import QMimeData
from PyQt5.QtWidgets import (QApplication, QDialog, QGridLayout, QLabel,QPushButton)
from PyQt5.QtGui import QPixmap
class Form(QDialog):
def __init__(self, parent=None):
super(Form, self).__init__(parent)
textCopyButton = QPushButton("&Copy Text")
textPasteButton = QPushButton("Paste &Text")
htmlCopyButton = QPushButton("C&opy HTML")
htmlPasteButton = QPushButton("Paste &HTML")
imageCopyButton = QPushButton("Co&py Image")
imagePasteButton = QPushButton("Paste &Image")
self.textLabel = QLabel("Original text")
self.imageLabel = QLabel()
self.imageLabel.setPixmap(QPixmap(os.path.join(
os.path.dirname(__file__), "images/clock.png")))
layout = QGridLayout()
layout.addWidget(textCopyButton, 0, 0)
layout.addWidget(imageCopyButton, 0, 1)
layout.addWidget(htmlCopyButton, 0, 2)
layout.addWidget(textPasteButton, 1, 0)
layout.addWidget(imagePasteButton, 1, 1)
layout.addWidget(htmlPasteButton, 1, 2)
layout.addWidget(self.textLabel, 2, 0, 1, 2)
layout.addWidget(self.imageLabel, 2, 2)
self.setLayout(layout)
textCopyButton.clicked.connect(self.copyText)
textPasteButton.clicked.connect(self.pasteText)
htmlCopyButton.clicked.connect(self.copyHtml)
htmlPasteButton.clicked.connect(self.pasteHtml)
imageCopyButton.clicked.connect(self.copyImage)
imagePasteButton.clicked.connect(self.pasteImage)
self.setWindowTitle("Clipboard範例")
def copyText(self):
clipboard = QApplication.clipboard()
clipboard.setText("I've been clipped!")
def pasteText(self):
clipboard = QApplication.clipboard()
self.textLabel.setText(clipboard.text())
def copyImage(self):
clipboard = QApplication.clipboard()
clipboard.setPixmap(QPixmap(os.path.join(
os.path.dirname(__file__), "./images/python.png")))
def pasteImage(self):
clipboard = QApplication.clipboard()
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()
if mimeData.hasHtml():
self.textLabel.setText(mimeData.html())
if __name__ == "__main__":
app = QApplication(sys.argv)
form = Form()
form.show()
sys.exit(app.exec_())

View File

@ -0,0 +1,52 @@
# -*- coding: utf-8 -*-
'''
簡介
PyQt5中DateTimeEdit範例
'''
import sys
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import QDate, QDateTime , QTime
class DateTimeEditDemo(QWidget):
def __init__(self):
super(DateTimeEditDemo, self).__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('QDateTimeEdit範例')
self.resize(300, 90)
vlayout = QVBoxLayout()
dateTimeEdit = QDateTimeEdit(self)
dateTimeEdit2 = QDateTimeEdit(QDateTime.currentDateTime(), self)
dateEdit = QDateTimeEdit(QDate.currentDate(), self)
timeEdit = QDateTimeEdit(QTime.currentTime(), self)
# 設定日期/時間格式
dateTimeEdit.setDisplayFormat("yyyy-MM-dd HH:mm:ss")
# dateTimeEdit.setDisplayFormat("yyyy/MM/dd HH:mm")
dateTimeEdit2.setDisplayFormat("yyyy/MM/dd HH-mm-ss")
# dateTimeEdit2.setDisplayFormat("yyyy/MM/dd HH:mm")
dateEdit.setDisplayFormat("yyyy.MM.dd")
# dateEdit.setDisplayFormat("yyyy/MM/dd")
timeEdit.setDisplayFormat("HH:mm:ss")
vlayout.addWidget( dateTimeEdit )
vlayout.addWidget( dateTimeEdit2)
vlayout.addWidget( dateEdit )
vlayout.addWidget( timeEdit )
self.setLayout(vlayout)
if __name__ == '__main__':
app = QApplication(sys.argv)
demo = DateTimeEditDemo()
demo.show()
sys.exit(app.exec_())

View File

@ -0,0 +1,84 @@
# -*- coding: utf-8 -*-
'''
簡介
PyQt5中DateTimeEdit範例
'''
import sys
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import QDate, QDateTime , QTime
class DateTimeEditDemo(QWidget):
def __init__(self):
super(DateTimeEditDemo, self).__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('QDateTimeEdit範例')
self.resize(300, 90)
vlayout = QVBoxLayout()
self.dateEdit = QDateTimeEdit(QDateTime.currentDateTime(), self)
self.dateEdit.setDisplayFormat("yyyy-MM-dd HH:mm:ss")
# 設定最小日期
self.dateEdit.setMinimumDate(QDate.currentDate().addDays(-365))
# 設定最大日期
self.dateEdit.setMaximumDate(QDate.currentDate().addDays(365))
self.dateEdit.setCalendarPopup( True)
self.dateEdit.dateChanged.connect(self.onDateChanged)
self.dateEdit.dateTimeChanged.connect(self.onDateTimeChanged)
self.dateEdit.timeChanged.connect(self.onTimeChanged)
self.btn = QPushButton('取得日期和時間')
self.btn.clicked.connect(self.onButtonClick)
vlayout.addWidget( self.dateEdit )
vlayout.addWidget( self.btn )
self.setLayout(vlayout)
# 日期發生改變時執行
def onDateChanged(self , date):
print(date)
# 無論是日期或時間發生改變時都會執行
def onDateTimeChanged(self , dateTime ):
print(dateTime)
# 時間發生改變時執行
def onTimeChanged(self , time):
print(time)
def onButtonClick(self ):
dateTime = self.dateEdit.dateTime()
# 最大日期
maxDate = self.dateEdit.maximumDate()
# 最大日期時間
maxDateTime = self.dateEdit.maximumDateTime()
# 最大時間
maxTime = self.dateEdit.maximumTime()
# 最小日期
minDate = self.dateEdit.minimumDate()
# 最小日期時間
minDateTime = self.dateEdit.minimumDateTime()
# 最小時間
minTime = self.dateEdit.minimumTime()
print('\n選擇的日期/時間:' )
print('dateTime=%s' % str(dateTime) )
print('maxDate=%s' % str(maxDate) )
print('maxDateTime=%s' % str(maxDateTime) )
print('maxTime=%s' % str(maxTime) )
print('minDate=%s' % str(minDate) )
print('minDateTime=%s' % str(minDateTime) )
print('minTime=%s' % str(minTime) )
if __name__ == '__main__':
app = QApplication(sys.argv)
demo = DateTimeEditDemo()
demo.show()
sys.exit(app.exec_())

View File

@ -0,0 +1,58 @@
# -*- coding: utf-8 -*-
'''
簡介
PyQt5中DateTimeEdit範例
'''
import sys
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import QDate, QDateTime , QTime
class DateTimeEditDemo(QWidget):
def __init__(self):
super(DateTimeEditDemo, self).__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('QDateTimeEdit範例')
self.resize(300, 90)
vlayout = QVBoxLayout()
dateTimeEdit = QDateTimeEdit(self)
# dateTimeEdit2 = QDateTimeEdit(QDateTime.currentDateTime(), self)
# dateEdit = QDateTimeEdit(QDate.currentDate(), self)
# timeEdit = QDateTimeEdit(QTime.currentTime(), self)
# 設定日期/時間格式
dateTimeEdit.setDisplayFormat("yyyy-MM-dd HH:mm:ss")
# dateTimeEdit.setDisplayFormat("yyyy/MM/dd HH:mm")
# dateTimeEdit2.setDisplayFormat("yyyy/MM/dd HH-mm-ss")
# dateTimeEdit2.setDisplayFormat("yyyy/MM/dd HH:mm")
# dateEdit.setDisplayFormat("yyyy.MM.dd")
# dateEdit.setDisplayFormat("yyyy/MM/dd")
# timeEdit.setDisplayFormat("HH:mm:ss")
vlayout.addWidget( dateTimeEdit )
# vlayout.addWidget( dateTimeEdit2)
# vlayout.addWidget( dateEdit )
# vlayout.addWidget( timeEdit )
self.btn4= QPushButton("取得日期和時間")
self.btn4.setDefault(True)
# self.btn4.clicked.connect(lambda:self.whichbtn(self.btn4))
vlayout.addWidget(self.btn4)
self.setLayout(vlayout)
if __name__ == '__main__':
app = QApplication(sys.argv)
demo = DateTimeEditDemo()
demo.show()
sys.exit(app.exec_())