#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 测试2D GUI组件显示修复 验证按钮、标签、输入框是否能正常显示 """ import warnings warnings.filterwarnings("ignore", category=DeprecationWarning) from direct.showbase.ShowBase import ShowBase from direct.gui.DirectGui import * from panda3d.core import * import os class GUITestApp(ShowBase): def __init__(self): ShowBase.__init__(self) # 设置背景色 self.setBackgroundColor(0.2, 0.2, 0.3) # 尝试加载中文字体 self.chinese_font = self.loadChineseFont() # 创建测试GUI元素 self.createTestGUI() print("GUI测试应用启动成功!") print(f"中文字体加载状态: {'成功' if self.chinese_font else '失败,使用默认字体'}") def loadChineseFont(self): """尝试加载中文字体""" font_paths = [ '/usr/share/fonts/truetype/wqy/wqy-microhei.ttc', '/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc', '/usr/share/fonts/truetype/arphic/ukai.ttc', '/usr/share/fonts/truetype/arphic/uming.ttc', ] for font_path in font_paths: if os.path.exists(font_path): try: font = self.loader.loadFont(font_path) if font: return font except: continue return None def createTestGUI(self): """创建测试GUI元素""" # 标题 self.title = OnscreenText( text="2D GUI 显示测试", pos=(0, 0.9), scale=0.08, fg=(1, 1, 1, 1), align=TextNode.ACenter, font=self.chinese_font if self.chinese_font else None ) # 测试不同位置的GUI元素 positions = [ ("中心", (0, 0, 0)), ("左侧", (-5, 0, 0)), ("右侧", (5, 0, 0)), ("上方", (0, 0, 5)), ("下方", (0, 0, -5)) ] self.gui_elements = [] y_offset = 0.6 for i, (name, pos) in enumerate(positions): # 计算屏幕位置(模拟修复后的坐标转换) gui_pos = (pos[0] * 0.1, 0, pos[2] * 0.1) # 创建按钮 button = DirectButton( text=f"{name}按钮", pos=gui_pos, scale=0.08, command=self.onButtonClick, extraArgs=[name], frameColor=(0.2, 0.6, 0.8, 1), text_font=self.chinese_font if self.chinese_font else None, rolloverSound=None, clickSound=None ) # 偏移位置创建标签 label_pos = (gui_pos[0], 0, gui_pos[2] - 0.2) label = DirectLabel( text=f"{name}标签", pos=label_pos, scale=0.06, frameColor=(0, 0, 0, 0), text_fg=(1, 1, 0, 1), text_font=self.chinese_font if self.chinese_font else None ) # 偏移位置创建输入框 entry_pos = (gui_pos[0], 0, gui_pos[2] - 0.4) entry = DirectEntry( text="", pos=entry_pos, scale=0.05, command=self.onEntrySubmit, extraArgs=[name], initialText=f"{name}输入框", numLines=1, width=12, focus=0 ) self.gui_elements.extend([button, label, entry]) print(f"创建了 {name} 位置的GUI元素:") print(f" - 按钮位置: {gui_pos}") print(f" - 标签位置: {label_pos}") print(f" - 输入框位置: {entry_pos}") # 创建退出按钮 self.exitButton = DirectButton( text="退出测试", pos=(0, 0, -0.8), scale=0.06, command=self.exitTest, frameColor=(0.8, 0.2, 0.2, 1), text_font=self.chinese_font if self.chinese_font else None ) # 状态信息 self.statusText = OnscreenText( text="如果你能看到各个位置的按钮、标签和输入框,说明修复成功!", pos=(0, -0.9), scale=0.04, fg=(0, 1, 0, 1), align=TextNode.ACenter, font=self.chinese_font if self.chinese_font else None ) def onButtonClick(self, name): """按钮点击事件""" print(f"点击了 {name} 按钮") self.statusText.setText(f"最后点击: {name} 按钮") def onEntrySubmit(self, text, name): """输入框提交事件""" print(f"{name} 输入框内容: {text}") self.statusText.setText(f"{name} 输入框: {text}") def exitTest(self): """退出测试""" print("退出GUI测试") self.userExit() if __name__ == "__main__": print("启动2D GUI显示测试...") print("如果修复成功,你应该能看到不同位置的按钮、标签和输入框") app = GUITestApp() app.run()