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

This commit is contained in:
M11212051 2024-05-05 22:38:21 +08:00
parent 007fafe1b6
commit a70a9a6b08
2 changed files with 85 additions and 0 deletions

View File

@ -0,0 +1,61 @@
# -*- coding: utf-8 -*-
'''
簡介
PyQT5中QTimer範例
'''
from PyQt5.QtWidgets import QWidget, QPushButton , QApplication ,QListWidget, QGridLayout , QLabel
from PyQt5.QtCore import QTimer ,QDateTime
import sys
class WinForm(QWidget):
def __init__(self,parent=None):
super(WinForm,self).__init__(parent)
self.setWindowTitle("QTimer demo")
self.listFile= QListWidget()
self.label = QLabel('顯示目前時間')
self.startBtn = QPushButton('開始')
self.endBtn = QPushButton('結束')
layout = QGridLayout(self)
# 初始化計時器
self.timer = QTimer(self)
# showTime()方法
self.timer.timeout.connect(self.showTime)
layout.addWidget(self.label,0,0,1,2)
layout.addWidget(self.startBtn,1,0)
layout.addWidget(self.endBtn,1,1)
self.startBtn.clicked.connect( self.startTimer)
self.endBtn.clicked.connect( self.endTimer)
self.setLayout(layout)
def showTime(self):
# 取得系統現在的時間
time = QDateTime.currentDateTime()
# 設定系統時間顯示格式
timeDisplay = time.toString("yyyy-MM-dd hh:mm:ss dddd");
# 在標籤上顯示時間
self.label.setText( timeDisplay )
def startTimer(self):
# 設定時間間隔,並啟動計時器
self.timer.start(1000)
self.startBtn.setEnabled(False)
self.endBtn.setEnabled(True)
def endTimer(self):
self.timer.stop()
self.startBtn.setEnabled(True)
self.endBtn.setEnabled(False)
if __name__ == "__main__":
app = QApplication(sys.argv)
form = WinForm()
form.show()
sys.exit(app.exec_())

View File

@ -0,0 +1,24 @@
# -*- coding: utf-8 -*-
'''
簡介
PyQT5中QTimer關閉程式範例
'''
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
if __name__ == '__main__':
app = QApplication(sys.argv)
label = QLabel("<font color=red size=128><b>Hello PyQt視窗會在10秒後消失</b></font>")
# 無邊框視窗
label.setWindowFlags(Qt.SplashScreen|Qt.FramelessWindowHint)
label.show()
# 設定10秒後自動退出
QTimer.singleShot(10000, app.quit)
sys.exit(app.exec_())