#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 主程序GUI功能直接测试 使用工具栏按钮创建GUI元素并验证显示 """ import warnings warnings.filterwarnings("ignore", category=DeprecationWarning) import sys import os import time from PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBox from PyQt5.QtCore import QTimer # 导入主程序 from test import MyWorld, CustomPanda3DWidget def testMainProgramGUI(): """测试主程序的GUI功能""" print("=== 测试主程序GUI功能 ===") app = QApplication(sys.argv) # 创建主程序的世界对象 world = MyWorld() # 创建窗口 mainWindow = QMainWindow() mainWindow.setWindowTitle("主程序GUI测试") mainWindow.setGeometry(100, 100, 800, 600) # 创建Panda3D部件 pandaWidget = CustomPanda3DWidget(world) mainWindow.setCentralWidget(pandaWidget) # 显示窗口 mainWindow.show() # 使用定时器延迟创建GUI元素 def createTestGUIElements(): print("\n开始创建GUI元素...") try: # 使用不同的测试坐标 test_positions = [ (0, 0, 0), # 中心 (-5, 0, 0), # 左侧 (5, 0, 0), # 右侧 (0, 0, 5), # 上方 (0, 0, -5), # 下方 ] # 创建按钮 print("创建按钮...") for i, pos in enumerate(test_positions): button = world.createGUIButton(pos, f"按钮{i+1}", 0.08) print(f" 按钮{i+1}: 位置{pos} -> 屏幕位置{button.getPos() if button else 'None'}") # 创建标签 print("创建标签...") label_positions = [ (0, 0, -8), # 中心下方 (-8, 0, 0), # 左侧 (8, 0, 0), # 右侧 ] for i, pos in enumerate(label_positions): label = world.createGUILabel(pos, f"标签{i+1}", 0.06) print(f" 标签{i+1}: 位置{pos} -> 屏幕位置{label.getPos() if label else 'None'}") # 创建输入框 print("创建输入框...") entry_positions = [ (0, 0, -12), # 中心下方 (-10, 0, -8), # 左下 ] for i, pos in enumerate(entry_positions): entry = world.createGUIEntry(pos, f"输入框{i+1}", 0.05) print(f" 输入框{i+1}: 位置{pos} -> 屏幕位置{entry.getPos() if entry else 'None'}") # 创建3D元素作为对比 print("创建3D元素...") world.createGUI3DText((0, 5, 2), "3D测试文本", 0.5) world.createGUIVirtualScreen((3, 8, 0), (2, 1), "测试屏幕") print(f"\n总共创建了 {len(world.gui_elements)} 个GUI元素") # 检查aspect2d的子节点 print(f"\naspect2d子节点数量: {world.aspect2d.getNumChildren()}") for i in range(world.aspect2d.getNumChildren()): child = world.aspect2d.getChild(i) print(f" 子节点{i}: {child.getName()}") # 显示消息框告知结果 msg = f"""GUI元素创建完成! 创建的元素: - 5个按钮 - 3个标签 - 2个输入框 - 1个3D文本 - 1个虚拟屏幕 总计: {len(world.gui_elements)} 个 如果你看不到2D GUI元素(按钮、标签、输入框), 但能看到3D元素(黄色文本、虚拟屏幕), 说明坐标转换可能还有问题。 请观察窗口中的GUI元素。""" QMessageBox.information(mainWindow, "GUI创建完成", msg) except Exception as e: error_msg = f"创建GUI元素时出错: {str(e)}" print(error_msg) QMessageBox.critical(mainWindow, "错误", error_msg) # 2秒后创建GUI元素 QTimer.singleShot(2000, createTestGUIElements) print("窗口已显示,2秒后将创建GUI元素...") print("请观察是否能看到所有GUI组件") return app.exec_() if __name__ == "__main__": testMainProgramGUI()