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

This commit is contained in:
M11212051 2024-05-05 22:31:14 +08:00
parent 16b667068f
commit 9b144780d0
5 changed files with 193 additions and 0 deletions

View File

@ -0,0 +1,76 @@
# -*- coding: utf-8 -*-
'''
簡介
PyQT5中Qlabel範例
'''
from PyQt5.QtWidgets import QApplication, QLabel ,QWidget, QVBoxLayout
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap ,QPalette
import sys
class WindowDemo(QWidget):
def __init__(self ):
super().__init__()
label1 = QLabel(self)
label2 = QLabel(self)
label3 = QLabel(self)
label4 = QLabel(self)
#1 初始化標籤控制項
label1.setText("這是一個文字標籤。")
label1.setAutoFillBackground(True)
palette = QPalette()
palette.setColor(QPalette.Window,Qt.blue)
label1.setPalette(palette)
label1.setAlignment( Qt.AlignCenter)
label2.setText("<a href='#'>歡迎使用Python GUI應用程式</a>")
label3.setAlignment( Qt.AlignCenter)
label3.setToolTip('這是一個圖片標籤')
label3.setPixmap( QPixmap("./images/python.jpg"))
label4.setText("<A href='http://www.cnblogs.com/wangshuo1/'>歡迎連結信平的小屋</a>")
label4.setAlignment( Qt.AlignRight)
label4.setToolTip('這是一個超連結標籤')
#2 在視窗佈局中加入控制項
vbox = QVBoxLayout()
vbox.addWidget(label1)
vbox.addStretch()
vbox.addWidget(label2)
vbox.addStretch()
vbox.addWidget( label3 )
vbox.addStretch()
vbox.addWidget( label4)
#3 允許label1控制項存取超連結
label1.setOpenExternalLinks(True)
# 開放允許存取超連結,預設是不允許,必須透過 setOpenExternalLinks(True)開放
label4.setOpenExternalLinks( False )
# 點擊文字方塊繫結槽事件
label4.linkActivated.connect( link_clicked )
# 滑過文字方塊繫結槽事件
label2.linkHovered.connect( link_hovered )
label1.setTextInteractionFlags( Qt.TextSelectableByMouse )
self.setLayout(vbox)
self.setWindowTitle("QLabel範例")
def link_hovered():
print("當滑鼠滑過label-2標籤時觸發事件。")
def link_clicked():
print("當用滑鼠點擊label-4標籤時觸發事件。" )
if __name__ == "__main__":
app = QApplication(sys.argv)
win = WindowDemo()
win.show()
sys.exit(app.exec_())

View File

@ -0,0 +1,29 @@
# -*- coding: utf-8 -*-
'''
簡介
PyQT5中主視窗範例
'''
import sys
from PyQt5.QtWidgets import QMainWindow , QApplication
from PyQt5.QtGui import QIcon
# MainWidget
class MainWindow(QMainWindow):
def __init__(self,parent=None):
super(MainWindow,self).__init__(parent)
# 設定主視窗標題
self.setWindowTitle("PyQt QMainWindow範例")
self.resize(400, 200)
self.status = self.statusBar()
self.status.showMessage("這是狀態列提示",10000)
if __name__ == "__main__":
app = QApplication(sys.argv)
app.setWindowIcon(QIcon("./images/cartoon1.ico"))
main = MainWindow()
main.show()
sys.exit(app.exec_())

View File

@ -0,0 +1,30 @@
# -*- coding: utf-8 -*-
'''
簡介
展示程式圖示範例
'''
import sys
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QWidget , QApplication
#1
class Icon(QWidget):
def __init__(self, parent = None):
super(Icon,self).__init__(parent)
self.initUI()
#2
def initUI(self):
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('展示程式圖示範例')
self.setWindowIcon(QIcon('./images/cartoon1.ico'))
if __name__ == '__main__':
app = QApplication(sys.argv)
icon = Icon()
icon.show()
sys.exit(app.exec_())

View File

@ -0,0 +1,28 @@
# -*- coding: utf-8 -*-
'''
簡介
PyQT5中氣泡提示
'''
import sys
from PyQt5.QtWidgets import QWidget, QToolTip , QApplication
from PyQt5.QtGui import QFont
class Winform(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
QToolTip.setFont(QFont('SansSerif', 10)) # 新細明體
self.setToolTip('這是一個<b>氣泡提示</b>')
self.setGeometry(200, 300, 400, 400)
self.setWindowTitle('氣泡提示demo')
if __name__ == '__main__':
app = QApplication(sys.argv)
win = Winform()
win.show()
sys.exit(app.exec_())

View File

@ -0,0 +1,30 @@
# -*- coding: utf-8 -*-
'''
簡介
PyQT5將視窗放在螢幕中間例子
'''
from PyQt5.QtWidgets import QDesktopWidget, QApplication ,QMainWindow
import sys
class Winform( QMainWindow):
def __init__(self, parent=None):
super( Winform, self).__init__(parent)
self.setWindowTitle('主視窗放在螢幕中間例子')
self.resize(370,250)
self.center()
def center(self):
screen = QDesktopWidget().screenGeometry()
size = self.geometry()
self.move((screen.width() - size.width()) / 2, (screen.height() - size.height()) / 2)
if __name__ == "__main__":
app = QApplication(sys.argv)
win = Winform()
win.show()
sys.exit(app.exec_())