115 lines
3.2 KiB
Python
115 lines
3.2 KiB
Python
#!/usr/bin/env python3
|
||
"""
|
||
PyQt6界面测试脚本
|
||
验证PyQt6是否正确安装和运行
|
||
"""
|
||
|
||
import sys
|
||
|
||
def test_pyqt6_import():
|
||
"""测试PyQt6导入"""
|
||
try:
|
||
from PyQt6.QtWidgets import QApplication, QLabel, QMainWindow
|
||
from PyQt6.QtCore import Qt
|
||
from PyQt6.QtGui import QPixmap
|
||
print("✓ PyQt6核心模块导入成功")
|
||
return True
|
||
except ImportError as e:
|
||
print(f"✗ PyQt6导入失败: {e}")
|
||
return False
|
||
|
||
def test_basic_window():
|
||
"""测试基本窗口创建"""
|
||
try:
|
||
from PyQt6.QtWidgets import QApplication, QMainWindow, QLabel, QVBoxLayout, QWidget
|
||
|
||
app = QApplication(sys.argv)
|
||
|
||
window = QMainWindow()
|
||
window.setWindowTitle("PyQt6测试窗口")
|
||
window.setGeometry(100, 100, 400, 300)
|
||
|
||
central_widget = QWidget()
|
||
layout = QVBoxLayout(central_widget)
|
||
|
||
label = QLabel("PyQt6界面测试成功!")
|
||
label.setAlignment(Qt.AlignmentFlag.AlignCenter)
|
||
label.setStyleSheet("""
|
||
QLabel {
|
||
font-size: 18px;
|
||
font-weight: bold;
|
||
color: #4CAF50;
|
||
padding: 20px;
|
||
}
|
||
""")
|
||
|
||
layout.addWidget(label)
|
||
window.setCentralWidget(central_widget)
|
||
|
||
print("✓ PyQt6窗口创建成功")
|
||
print("✓ 测试窗口将显示2秒后自动关闭")
|
||
|
||
# 显示窗口2秒后自动关闭
|
||
window.show()
|
||
|
||
from PyQt6.QtCore import QTimer
|
||
timer = QTimer()
|
||
timer.singleShot(2000, app.quit)
|
||
|
||
app.exec()
|
||
print("✓ PyQt6窗口测试完成")
|
||
return True
|
||
|
||
except Exception as e:
|
||
print(f"✗ PyQt6窗口测试失败: {e}")
|
||
return False
|
||
|
||
def test_other_components():
|
||
"""测试其他PyQt6组件"""
|
||
try:
|
||
from PyQt6.QtWidgets import (QApplication, QListWidget, QPushButton,
|
||
QScrollArea, QGroupBox, QGridLayout)
|
||
from PyQt6.QtCore import QThread, pyqtSignal, QTimer
|
||
from PyQt6.QtGui import QPixmap, QColor
|
||
|
||
print("✓ PyQt6所有组件导入成功")
|
||
return True
|
||
except ImportError as e:
|
||
print(f"✗ PyQt6组件导入失败: {e}")
|
||
return False
|
||
|
||
def main():
|
||
"""主测试函数"""
|
||
print("=== PyQt6界面测试 ===\n")
|
||
|
||
tests = [
|
||
("PyQt6导入测试", test_pyqt6_import),
|
||
("PyQt6组件测试", test_other_components),
|
||
("PyQt6窗口测试", test_basic_window),
|
||
]
|
||
|
||
passed = 0
|
||
total = len(tests)
|
||
|
||
for test_name, test_func in tests:
|
||
print(f"执行 {test_name}...")
|
||
if test_func():
|
||
passed += 1
|
||
print()
|
||
|
||
print(f"=== 测试结果: {passed}/{total} 通过 ===")
|
||
|
||
if passed == total:
|
||
print("✅ 所有PyQt6测试通过,界面可以正常运行")
|
||
print("\n启动GUI界面:")
|
||
print("python3 monitor_gui.py")
|
||
return True
|
||
else:
|
||
print("❌ 部分测试失败,请检查PyQt6安装")
|
||
print("\n安装PyQt6:")
|
||
print("pip install PyQt6>=6.4.0")
|
||
return False
|
||
|
||
if __name__ == "__main__":
|
||
success = main()
|
||
sys.exit(0 if success else 1) |