EG/demo/chinese_gui_test.py
2025-07-02 09:49:59 +08:00

261 lines
8.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
PANDA3D 中文GUI支持测试
解决中文字体显示问题的示例
"""
from direct.showbase.ShowBase import ShowBase
from direct.gui.DirectGui import *
from direct.gui.OnscreenText import OnscreenText
from panda3d.core import *
import os
class ChineseGUITest(ShowBase):
def __init__(self):
ShowBase.__init__(self)
# 设置窗口属性
wp = WindowProperties()
wp.setTitle("PANDA3D Chinese GUI Test")
self.win.requestProperties(wp)
self.setBackgroundColor(0.1, 0.2, 0.4)
# 尝试加载中文字体
self.chinese_font = self.loadChineseFont()
# 创建GUI组件
self.createGUI()
print("="*50)
print("PANDA3D 中文GUI测试程序启动")
print(f"中文字体加载状态: {'成功' if self.chinese_font else '失败,使用默认字体'}")
print("="*50)
def loadChineseFont(self):
"""尝试加载中文字体"""
# 常见的中文字体路径列表
font_paths = [
# Ubuntu/Debian系统的中文字体
'/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',
'/usr/share/fonts/truetype/droid/DroidSansFallbackFull.ttf',
'/usr/share/fonts/truetype/noto/NotoSansCJK-Regular.ttc',
'/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf',
# 其他可能的字体路径
'/System/Library/Fonts/PingFang.ttc', # macOS
'C:/Windows/Fonts/msyh.ttc', # Windows
'C:/Windows/Fonts/simhei.ttf', # Windows
]
print("正在尝试加载中文字体...")
for font_path in font_paths:
if os.path.exists(font_path):
try:
print(f"尝试加载字体: {font_path}")
font = self.loader.loadFont(font_path)
if font:
print(f"成功加载字体: {font_path}")
return font
else:
print(f"字体文件存在但加载失败: {font_path}")
except Exception as e:
print(f"加载字体时出错: {font_path}, 错误: {e}")
print("未找到可用的中文字体,将使用默认字体")
print("如果中文显示为方块,请安装中文字体:")
print("sudo apt-get install fonts-wqy-microhei fonts-wqy-zenhei")
return None
def createText(self, text, pos, scale=0.06, fg=(1, 1, 1, 1)):
"""创建文本,优先使用中文字体"""
if self.chinese_font:
return OnscreenText(
text=text,
pos=pos,
scale=scale,
fg=fg,
align=TextNode.ALeft,
font=self.chinese_font
)
else:
return OnscreenText(
text=text,
pos=pos,
scale=scale,
fg=fg,
align=TextNode.ALeft
)
def createButton(self, text, pos, scale=0.06, command=None, frameColor=(0.5, 0.5, 0.5, 1)):
"""创建支持中文字体的按钮"""
return DirectButton(
text=text,
pos=pos,
scale=scale,
command=command,
frameColor=frameColor,
text_fg=(1, 1, 1, 1),
text_font=self.chinese_font if self.chinese_font else None
)
def createGUI(self):
"""创建GUI组件"""
# 标题
self.title = self.createText(
"PANDA3D 中文GUI测试",
(0, 0.85),
0.08,
(1, 1, 0, 1)
)
self.title.setAlign(TextNode.ACenter)
# 说明文本
self.info = self.createText(
"这个程序测试中文字体在PANDA3D中的显示效果",
(-0.95, 0.7),
0.05,
(0.8, 0.8, 0.8, 1)
)
# 创建测试按钮 - 支持中文字体
self.testButton = self.createButton(
text="Test Button (测试按钮)",
pos=(-0.6, 0, 0.5),
scale=0.08,
command=self.onTestClick,
frameColor=(0.2, 0.7, 0.2, 1)
)
# 创建标签显示区域
self.statusLabel = self.createText(
"状态: 程序就绪",
(-0.95, 0.3),
0.06,
(0, 1, 0, 1)
)
# 创建功能测试区域
self.createTestArea()
# 创建字体信息显示
self.createFontInfo()
# 退出按钮 - 支持中文字体
self.exitButton = self.createButton(
text="Exit (退出)",
pos=(0.7, 0, -0.7),
scale=0.06,
command=self.exitProgram,
frameColor=(0.8, 0.2, 0.2, 1)
)
def createTestArea(self):
"""创建测试区域"""
# 中文测试文本
chinese_texts = [
"简体中文测试:你好世界!",
"常用汉字:一二三四五六七八九十",
"标点符号:,。!?;:""''",
"数字混合2023年12月25日",
"英中混合Hello 世界 World 中国"
]
self.test_texts = []
for i, text in enumerate(chinese_texts):
text_obj = self.createText(
text,
(-0.95, 0.1 - i*0.08),
0.05,
(1, 1, 1, 1)
)
self.test_texts.append(text_obj)
# 计数器
self.clickCount = 0
self.counterText = self.createText(
f"点击次数: {self.clickCount}",
(-0.95, -0.5),
0.06,
(1, 0.5, 0, 1)
)
def createFontInfo(self):
"""创建字体信息显示"""
font_status = "已加载中文字体" if self.chinese_font else "使用默认字体"
self.fontInfo = self.createText(
f"字体状态: {font_status}",
(0.2, 0.7),
0.05,
(0.8, 0.8, 0.2, 1)
)
# 显示字体路径信息
if self.chinese_font:
# 尝试获取字体信息
try:
font_name = self.chinese_font.getName()
self.fontPath = self.createText(
f"字体名称: {font_name}",
(0.2, 0.6),
0.04,
(0.6, 0.6, 0.6, 1)
)
except:
self.fontPath = self.createText(
"字体信息: 无法获取",
(0.2, 0.6),
0.04,
(0.6, 0.6, 0.6, 1)
)
else:
self.fontPath = self.createText(
"建议安装: fonts-wqy-microhei",
(0.2, 0.6),
0.04,
(1, 0.5, 0.5, 1)
)
def onTestClick(self):
"""测试按钮点击事件"""
self.clickCount += 1
# 更新状态和计数器
self.statusLabel.setText(f"状态: 按钮被点击了 {self.clickCount}")
self.counterText.setText(f"点击次数: {self.clickCount}")
# 循环变换测试文本
test_messages = [
"第一次点击欢迎使用PANDA3D",
"第二次点击:中文显示测试正常",
"第三次点击:功能运行良好",
"继续点击:一切正常运行中..."
]
if self.clickCount <= len(test_messages):
message = test_messages[self.clickCount - 1]
else:
message = f"{self.clickCount}次点击:持续测试中..."
# 更新第一行测试文本
if self.test_texts:
self.test_texts[0].setText(message)
print(f"按钮点击: {self.clickCount} 次, 消息: {message}")
def exitProgram(self):
"""退出程序"""
print("用户选择退出程序")
print("再见Goodbye!")
self.userExit()
if __name__ == "__main__":
print("启动PANDA3D中文GUI测试程序...")
print("如果看到方块字符,说明需要安装中文字体")
app = ChineseGUITest()
app.run()