3/5/2025小於 1 分鐘
網站前端技術應用使用的參考教材。
2/19/2025小於 1 分鐘
本單元主要為課程簡介與開發環境設定。
2/19/2025小於 1 分鐘
本單元將介紹如何開發一個圖形界面的任務清單應用。
12/10/2024小於 1 分鐘
學習內容:
- 使用 PyQt6 程式載入並連接 UI。
- 設定相關的訊號處理函式
1. 匯入模組
from PyQt6.QtWidgets import QApplication, QWidget, QMessageBox
from PyQt6 import uic
import sys
12/10/2024大约 2 分鐘
學習內容:
- 使用 Qt Designer 設計簡單的 GUI。
- 添加元件(PushButton)並設定屬性。
- 使用 PyQt6 程式載入並連接 UI。
核心概念:
-
Qt Designer: Qt Designer 是一個專門設計 GUI 的工具,能快速構建視覺化的應用介面,並導出
.ui
文件供程式使用。 -
PyQt6: PyQt6 提供了載入
.ui
文件的方法,讓程式可以控制介面中的元件。 -
信號與槽: PyQt 使用信號與槽(Signal and Slot)機制,連接 GUI 的按鈕事件與程式功能。
12/10/2024大约 2 分鐘
12/10/2024大约 4 分鐘
學習內容
- 使用 Python Class 的基本用法。
- 了解
__name__ == '__main__'
的用法。 - 用 PyQt6 建立基本主視窗。
- 添加計數功能。
class
的用法
在 Python 中,class
是用來定義類別 (Class) 的關鍵字,類別是物件導向編程 (Object-Oriented Programming, OOP) 中的一個基礎概念。類別是對現實世界事物的抽象,可以包含變數(稱為屬性)和函式(稱為方法)。
12/3/2024大约 5 分鐘
學習內容
- 添加文字輸入框與按鈕。
- 顯示用戶輸入的文字。
- 使用按鈕清除輸入框文字內容。
程式碼範例
from PyQt6.QtWidgets import (
QApplication, QLabel, QLineEdit,
QPushButton, QVBoxLayout, QWidget, QMessageBox
)
import sys
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("PyQt6 輸入與互動")
self.resize(400, 300)
# 元件:標籤、輸入框、按鈕
self.label = QLabel("請輸入文字並點擊按鈕:", self)
self.input_field = QLineEdit(self)
self.input_field.setPlaceholderText("在這裡輸入...")
self.display_button = QPushButton("顯示文字", self)
self.clear_button = QPushButton("清除文字", self)
# 設定佈局
layout = QVBoxLayout()
layout.addWidget(self.label)
layout.addWidget(self.input_field)
layout.addWidget(self.display_button)
layout.addWidget(self.clear_button)
self.setLayout(layout)
# 事件連接
self.display_button.clicked.connect(self.display_text)
self.clear_button.clicked.connect(self.clear_text)
def display_text(self):
text = self.input_field.text()
if text.strip():
self.label.setText(f"您輸入了:{text}")
else:
QMessageBox.warning(self, "警告", "輸入欄位為空!")
def clear_text(self):
self.input_field.clear()
self.label.setText("請輸入文字並點擊按鈕:")
# 主程式入口
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec())
12/3/2024大约 2 分鐘