如何通过 python 库使用非欧洲语言
我对编程比较陌生,最近我开始使用 pygame(一组用 python 编写游戏的模块)。我正在创建一个程序/游戏,其中一些标签、字符串、按钮等都是阿拉伯语的。我猜 pygame 必须支持阿拉伯字母,但它可能不支持?或者我可以使用另一个支持阿拉伯语的 GUI 库并与 pygame 一起使用吗?任何方向将不胜感激!
I'm relatively new to programming and recently I've started playing around with pygame (set of modules to write games in python). I'm looking to create a program/game in which some of the labels, strings, buttons, etc are in Arabic. I'm guessing that pygame has to support Arabic letters and it probably doesn't? Or could I possibly use another GUI library that does support Arabic and use that in unison with pygame? Any direction would be much appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Python 本身对所有内容都使用 Unicode,所以这不是问题。快速谷歌搜索还表明 PyGame 应该能够很好地渲染 Unicode 字体。所以我认为问题更多在于它找不到用于渲染的特定语言的字体。
这里是 PyGame 的一个简短示例,尤其是 此链接应该很有用。
这是重要的库 - 因此指定一种可以呈现您的语言的字体用它来渲染它应该可以正常工作。编写一个小包装器可能是个好主意
注意:我自己没有使用过 PyGame,因此这是基于猜测和一些有关 PyGame 如何呈现字体的快速搜索。
PS:如果您希望游戏能够为所有用户可靠地运行,那么在您的版本中包含开源字体可能是个好主意,否则您需要一些方法来检查用户是否安装了一些可以正常运行的字体 -如果您需要 Xplattform 支持,这可能是一个不小的问题。
Well Python itself uses Unicode for everything so that's not the problem. A quick googling also shows that PyGame should be able to render Unicode fonts just fine. So I assume the problem is more that it can't find fonts for the specific language to use for rendering.
Here is a short example for PyGame and especially this link should be useful.
This is the important library - so specifying a font that can render your language and using it to render it should work fine. Probably a good idea to write a small wrapper
Nb: Haven't used PyGame myself so this is based on speculation and some quick search about how PyGame renders fonts.
PS: If you want the game to work reliably for all of your users, it's probably a good idea to include an Open Source font in your release, otherwise you need some methodology to check if the user has some fonts installed that will work fine - a probably non-trivial problem if you want Xplattform support.
Python 支持 unicode 编码源。
在文件的开头使用
#coding: [yourCoding]
形式的行将源文件的编码设置为正确的类型。我认为#coding: utf-8
适用于阿拉伯语。然后在字符串前面加上一个
u
,如下所示:(抱歉,如果您没有安装日语字体,这是我唯一方便的字体!)
这使得 python 将它们视为 unicode 字符。 此网站上有更多关于阿拉伯语的信息。
Python does support unicode-coded source.
Set the coding of your source file to the right type with a line of the form
# coding: [yourCoding]
at the very beginning of your file. I think# coding: utf-8
works for Arabic.Then prepend your string literals with a
u
, like so:(Sorry if you don't have a Japanese font installed, it's the only one I had handy!)
This makes python treat them as unicode characters. There's further information specific to Arabic on this site.
前面的两个答案都很棒。
还有一个很棒的内置 Python 函数,称为 unicode。它非常容易使用。
我想编写希伯来语文本,所以我编写了一个函数:
然后您可以使用以下方式调用它:
它会返回希伯来语编码的文本。
Both previous answers are great.
There is also a great built-in python function called unicode. It's very easy to use.
I wanted to write Hebrew text so I wrote a function:
Then you can call it using:
And it will return your text Hebrew encoded.