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

This commit is contained in:
M11212051 2024-05-05 22:38:00 +08:00
parent 350dd802eb
commit 007fafe1b6
5 changed files with 307 additions and 0 deletions

View File

@ -0,0 +1,70 @@
# -*- coding: utf-8 -*-
'''
簡介
PyQT5中QStackedWidget範例
'''
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class StackedExample(QWidget):
def __init__(self):
super(StackedExample, self).__init__()
self.setGeometry(300, 50, 10,10)
self.setWindowTitle('StackedWidget範例')
self.leftlist = QListWidget ()
self.leftlist.insertItem (0, '聯絡方式' )
self.leftlist.insertItem (1, '個人資料' )
self.leftlist.insertItem (2, '教育程度' )
self.stack1= QWidget()
self.stack2= QWidget()
self.stack3= QWidget()
self.stack1UI()
self.stack2UI()
self.stack3UI()
self.Stack = QStackedWidget (self)
self.Stack.addWidget (self.stack1)
self.Stack.addWidget (self.stack2)
self.Stack.addWidget (self.stack3)
hbox = QHBoxLayout(self)
hbox.addWidget(self.leftlist)
hbox.addWidget(self.Stack)
self.setLayout(hbox)
self.leftlist.currentRowChanged.connect(self.display)
def stack1UI(self):
layout=QFormLayout()
layout.addRow("姓名",QLineEdit())
layout.addRow("地址",QLineEdit())
self.stack1.setLayout(layout)
def stack2UI(self):
layout=QFormLayout()
sex=QHBoxLayout()
sex.addWidget(QRadioButton(""))
sex.addWidget(QRadioButton(""))
layout.addRow(QLabel("性別"),sex)
layout.addRow("生日",QLineEdit())
self.stack2.setLayout(layout)
def stack3UI(self):
layout=QHBoxLayout()
layout.addWidget(QLabel("科目"))
layout.addWidget(QCheckBox("物理"))
layout.addWidget(QCheckBox("數學"))
self.stack3.setLayout(layout)
def display(self,i):
self.Stack.setCurrentIndex(i)
if __name__ == '__main__':
app = QApplication(sys.argv)
demo = StackedExample()
demo.show()
sys.exit(app.exec_())

View File

@ -0,0 +1,58 @@
# -*- coding: utf-8 -*-
'''
簡介
PyQT5中QTabWidget範例
'''
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class TabDemo(QTabWidget):
def __init__(self, parent=None):
super(TabDemo, self).__init__(parent)
self.tab1 = QWidget()
self.tab2 = QWidget()
self.tab3 = QWidget()
self.addTab(self.tab1,"Tab 1")
self.addTab(self.tab2,"Tab 2")
self.addTab(self.tab3,"Tab 3")
self.tab1UI()
self.tab2UI()
self.tab3UI()
self.setWindowTitle("Tab範例")
def tab1UI(self):
layout = QFormLayout()
layout.addRow("姓名",QLineEdit())
layout.addRow("地址",QLineEdit())
self.setTabText(0,"聯絡方式")
self.tab1.setLayout(layout)
def tab2UI(self):
layout = QFormLayout()
sex = QHBoxLayout()
sex.addWidget(QRadioButton(""))
sex.addWidget(QRadioButton(""))
layout.addRow(QLabel("性別"),sex)
layout.addRow("生日",QLineEdit())
self.setTabText(1,"個人詳細資料")
self.tab2.setLayout(layout)
def tab3UI(self):
layout=QHBoxLayout()
layout.addWidget(QLabel("科目"))
layout.addWidget(QCheckBox("物理"))
layout.addWidget(QCheckBox("數學"))
self.setTabText(2,"教育程度")
self.tab3.setLayout(layout)
if __name__ == '__main__':
app = QApplication(sys.argv)
demo = TabDemo()
demo.show()
sys.exit(app.exec_())

View File

@ -0,0 +1,50 @@
# -*- coding: utf-8 -*-
'''
簡介
PyQT5中QThread範例
'''
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
global sec
sec=0
def setTime():
global sec
sec+=1
# LED顯示數字+1
lcdNumber.display(sec)
def work():
# 計時器每秒計數
timer.start(1000)
for i in range(2000000000):
pass
timer.stop()
if __name__ == "__main__":
app = QApplication(sys.argv)
top = QWidget()
top.resize(300,120)
# 垂直佈局類別
layout = QVBoxLayout(top)
# 增加一個顯示面板
lcdNumber = QLCDNumber()
layout.addWidget(lcdNumber)
button=QPushButton("測試")
layout.addWidget(button)
timer = QTimer()
# 每次計時結束觸發setTime
timer.timeout.connect(setTime)
button.clicked.connect(work)
top.show()
sys.exit(app.exec_())

View File

@ -0,0 +1,70 @@
# -*- coding: utf-8 -*-
'''
簡介
PyQT5中QThread範例
'''
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
global sec
sec=0
class WorkThread(QThread):
trigger = pyqtSignal()
def __int__(self):
super(WorkThread,self).__init__()
def run(self):
for i in range(2000000000):
pass
# 迴圈完畢後發射訊號
self.trigger.emit()
def countTime():
global sec
sec += 1
# LED顯示數字+1
lcdNumber.display(sec)
def work():
# 計時器每秒計數
timer.start(1000)
# 計時開始
workThread.start()
# 當收到迴圈完畢的訊號時,停止計數
workThread.trigger.connect(timeStop)
def timeStop():
timer.stop()
print("執行結束,耗時:",lcdNumber.value())
global sec
sec=0
if __name__ == "__main__":
app = QApplication(sys.argv)
top = QWidget()
top.resize(300,120)
# 垂直佈局類別QVBoxLayout
layout = QVBoxLayout(top)
# 增加一個顯示面板
lcdNumber = QLCDNumber()
layout.addWidget(lcdNumber)
button = QPushButton("測試")
layout.addWidget(button)
timer = QTimer()
workThread = WorkThread()
button.clicked.connect(work)
# 每次計時結束觸發countTime
timer.timeout.connect(countTime)
top.show()
sys.exit(app.exec_())

View File

@ -0,0 +1,59 @@
# -*- coding: utf-8 -*-
'''
簡介
PyQT5中QThread範例
'''
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys
class MainWidget(QWidget):
def __init__(self,parent=None):
super(MainWidget,self).__init__(parent)
self.setWindowTitle("QThread範例")
self.thread = Worker()
self.listFile = QListWidget()
self.btnStart = QPushButton('開始')
layout = QGridLayout(self)
layout.addWidget(self.listFile,0,0,1,2)
layout.addWidget(self.btnStart,1,1)
self.btnStart.clicked.connect( self.slotStart )
self.thread.sinOut.connect(self.slotAdd)
def slotAdd(self,file_inf):
self.listFile.addItem(file_inf)
def slotStart(self):
self.btnStart.setEnabled(False)
self.thread.start()
class Worker(QThread):
sinOut = pyqtSignal(str)
def __init__(self,parent=None):
super(Worker,self).__init__(parent)
self.working = True
self.num = 0
def __del__(self):
self.working = False
self.wait()
def run(self):
while self.working == True:
file_str = 'File index {0}'.format(self.num)
self.num += 1
# 發射訊號
self.sinOut.emit(file_str)
# 執行緒休眠2秒
self.sleep(2)
if __name__ == "__main__":
app = QApplication(sys.argv)
demo = MainWidget()
demo.show()
sys.exit(app.exec_())