#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ PANDA3D 基础GUI演示 最简单的DirectGUI使用示例 """ from direct.showbase.ShowBase import ShowBase from direct.gui.DirectGui import DirectButton, DirectLabel from direct.gui.OnscreenText import OnscreenText from panda3d.core import TextNode class BasicGUIDemo(ShowBase): def __init__(self): ShowBase.__init__(self) # 设置背景颜色 self.setBackgroundColor(0.2, 0.4, 0.6) # 尝试加载中文字体 self.chinese_font = self.loadChineseFont() # 创建标题 self.title = OnscreenText( text="PANDA3D DirectGUI 基础演示", pos=(0, 0.8), scale=0.1, fg=(1, 1, 1, 1), align=TextNode.ACenter, font=self.chinese_font if self.chinese_font else None ) # 创建一个简单按钮 self.button = DirectButton( text="点击我!", pos=(0, 0, 0.2), scale=0.1, command=self.buttonClicked, frameColor=(0.2, 0.8, 0.2, 1), text_font=self.chinese_font if self.chinese_font else None ) # 创建标签显示状态 self.statusLabel = DirectLabel( text="状态: 等待点击", pos=(0, 0, -0.2), scale=0.08, frameColor=(0, 0, 0, 0), text_fg=(1, 1, 0, 1), text_font=self.chinese_font if self.chinese_font else None ) # 创建退出按钮 self.exitButton = DirectButton( text="退出", pos=(0, 0, -0.5), scale=0.08, command=self.exitDemo, frameColor=(0.8, 0.2, 0.2, 1), text_font=self.chinese_font if self.chinese_font else None ) # 点击计数器 self.clickCount = 0 print("PANDA3D 基础GUI演示启动成功!") print("请点击窗口中的按钮来测试GUI功能") def buttonClicked(self): """按钮点击处理函数""" self.clickCount += 1 self.statusLabel['text'] = f"状态: 按钮被点击了 {self.clickCount} 次" print(f"按钮被点击! 总共点击了 {self.clickCount} 次") # 改变按钮颜色作为反馈 if self.clickCount % 3 == 0: self.button['frameColor'] = (0.8, 0.2, 0.8, 1) # 紫色 elif self.clickCount % 3 == 1: self.button['frameColor'] = (0.2, 0.8, 0.2, 1) # 绿色 else: self.button['frameColor'] = (0.2, 0.2, 0.8, 1) # 蓝色 def exitDemo(self): """退出演示""" print("用户选择退出") self.userExit() def loadChineseFont(self): """尝试加载中文字体""" import os 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 if __name__ == "__main__": print("正在启动PANDA3D基础GUI演示...") demo = BasicGUIDemo() demo.run()