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

This commit is contained in:
M11212051 2024-05-05 22:31:59 +08:00
parent 330504bb5f
commit b3af9b76ae
5 changed files with 217 additions and 0 deletions

View File

@ -0,0 +1,45 @@
# -*- coding: utf-8 -*-
'''
簡介
PyQt5中QComboBox範例
'''
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class ComboxDemo(QWidget):
def __init__(self, parent=None):
super(ComboxDemo, self).__init__(parent)
self.setWindowTitle("combox 範例")
self.resize(300, 90)
layout = QVBoxLayout()
self.lbl = QLabel("" )
self.cb = QComboBox()
self.cb.addItem("C")
self.cb.addItem("C++")
self.cb.addItems(["Java", "C#", "Python"])
self.cb.currentIndexChanged.connect(self.selectionchange)
layout.addWidget(self.cb)
layout.addWidget(self.lbl )
self.setLayout(layout)
def selectionchange(self,i):
self.lbl.setText( self.cb.currentText() )
self.lbl.adjustSize()
print( "Items in the list are :" )
for count in range(self.cb.count()):
print( 'item'+str(count) + '='+ self.cb.itemText(count) )
print( "Current index",i,"selection changed ",self.cb.currentText() )
if __name__ == '__main__':
app = QApplication(sys.argv)
comboxDemo = ComboxDemo()
comboxDemo.show()
sys.exit(app.exec_())

View File

@ -0,0 +1,37 @@
# -*- coding: utf-8 -*-
'''
簡介
PyQt5中QSpinBox範例
'''
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class spindemo(QWidget):
def __init__(self, parent=None):
super(spindemo, self).__init__(parent)
self.setWindowTitle("SpinBox 範例")
self.resize(300, 100)
layout = QVBoxLayout()
self.l1=QLabel("current value:")
self.l1.setAlignment(Qt.AlignCenter)
layout.addWidget(self.l1)
self.sp = QSpinBox()
layout.addWidget(self.sp)
self.sp.valueChanged.connect(self.valuechange)
self.setLayout(layout)
def valuechange(self):
self.l1.setText("目前值:" + str(self.sp.value()) )
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = spindemo()
ex.show()
sys.exit(app.exec_())

View File

@ -0,0 +1,53 @@
# -*- coding: utf-8 -*-
'''
簡介
PyQt5中QSlider範例
'''
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class SliderDemo(QWidget):
def __init__(self, parent=None):
super(SliderDemo, self).__init__(parent)
self.setWindowTitle("QSlider 範例")
self.resize(300, 100)
layout = QVBoxLayout()
self.l1 = QLabel("Hello PyQt5")
self.l1.setAlignment(Qt.AlignCenter)
layout.addWidget(self.l1)
# 水平方向
self.sl = QSlider(Qt.Horizontal)
# 設定最小值
self.sl.setMinimum(10)
# 設定最大值
self.sl.setMaximum(50)
# 步長
self.sl.setSingleStep( 3 )
# 設置目前值
self.sl.setValue(20)
# 刻度位置,刻度在下方
self.sl.setTickPosition(QSlider.TicksBelow)
# 設定刻度間隔
self.sl.setTickInterval(5)
layout.addWidget(self.sl)
# 連接訊號/槽
self.sl.valueChanged.connect(self.valuechange)
self.setLayout(layout)
def valuechange(self):
print('current slider value=%s' % self.sl.value() )
size = self.sl.value()
self.l1.setFont(QFont("Arial",size))
if __name__ == '__main__':
app = QApplication(sys.argv)
demo = SliderDemo()
demo.show()
sys.exit(app.exec_())

View File

@ -0,0 +1,42 @@
# -*- coding: utf-8 -*-
'''
簡介
PyQt5中Qmenu範例
'''
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class MenuDemo(QMainWindow):
def __init__(self, parent=None):
super(MenuDemo, self).__init__(parent)
layout = QHBoxLayout()
bar = self.menuBar()
file = bar.addMenu("File")
file.addAction("New")
save = QAction("Save", self)
save.setShortcut("Ctrl+S")
file.addAction(save)
edit = file.addMenu("Edit")
edit.addAction("Copy")
edit.addAction("Paste")
quit = QAction("Quit", self)
file.addAction(quit)
file.triggered[QAction].connect(self.processtrigger)
self.setLayout(layout)
self.setWindowTitle("Qmenu範例")
self.resize(350,300)
def processtrigger(self,q):
print( q.text()+" is triggered" )
if __name__ == '__main__':
app = QApplication(sys.argv)
demo = MenuDemo()
demo.show()
sys.exit(app.exec_())

View File

@ -0,0 +1,40 @@
# -*- coding: utf-8 -*-
'''
簡介
PyQt5中QToolBar範例
'''
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class ToolBarDemo( QMainWindow ):
def __init__(self, parent=None):
super(ToolBarDemo, self).__init__(parent)
self.setWindowTitle("QToolBar範例")
self.resize(300, 200)
layout = QVBoxLayout()
tb = self.addToolBar("File")
new = QAction(QIcon("./images/new.png"),"New",self)
tb.addAction(new)
open = QAction(QIcon("./images/open.png"),"Open",self)
tb.addAction(open)
save = QAction(QIcon("./images/save.png"),"Save",self)
tb.addAction(save)
tb.actionTriggered[QAction].connect(self.toolbtnpressed)
self.setLayout(layout)
def toolbtnpressed(self,a):
print("pressed tool button is ",a.text() )
if __name__ == '__main__':
app = QApplication(sys.argv)
demo = ToolBarDemo()
demo.show()
sys.exit(app.exec_())