好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

python设置中文界面实例方法

今天带来python设置中文界面实例方法教程详解

下面,小编将通过一组实例演示,让大家更直观,更清楚明白的了解要设置中文这一内容的操作步骤。

首先展示实例代码:

import pygame from pygame.locals import * def main(): pygame.init() screen = pygame.display.set_mode((1000, 450)) #窗口的大小 pygame.display.set_caption('pygame程序的界面的中文设置') #窗口标题,中文不需要特别的设置 background = pygame.Surface(screen.get_size()) background = background.convert() background.fill((250, 250, 250)) font = pygame.font.Font(None, 60) #原始代码,使用默认字体,不能显示中文 #font = pygame.font.Font('/home/xgj/Desktop/simsun/simsun.ttf', 60) #显示中文的设置和字体,及路径 text = font.render("Hello 我爱你", 1, (10, 10, 10)) textpos = text.get_rect() textpos.center = background.get_rect().center background.blit(text, textpos) screen.blit(background, (0, 0)) pygame.display.flip() while 1: for event in pygame.event.get(): if event.type == QUIT: return screen.blit(background, (0, 0)) pygame.display.flip() if __name__ == '__main__': main()

运行效果展示:

注意:hello后面是乱码,中文内容“我爱你”并没有显示。

修改后的代码展示:

import pygame from pygame.locals import * def main(): pygame.init() screen = pygame.display.set_mode((1000, 450)) #窗口的大小 pygame.display.set_caption('pygame程序的界面的中文设置') #窗口标题,中文不需要特别的设置 background = pygame.Surface(screen.get_size()) background = background.convert() background.fill((250, 250, 250)) #font = pygame.font.Font(None, 60) #原始代码,使用默认字体,不能显示中文 font = pygame.font.Font('/home/xgj/Desktop/simsun/simsun.ttf', 60) #显示中文的设置和字体,及路径 text = font.render("Hello 我爱你", 1, (10, 10, 10)) textpos = text.get_rect() textpos.center = background.get_rect().center background.blit(text, textpos) screen.blit(background, (0, 0)) pygame.display.flip() while 1: for event in pygame.event.get(): if event.type == QUIT: return screen.blit(background, (0, 0)) pygame.display.flip() if __name__ == '__main__': main()

运行效果展示:

从上面可以看出,已经显示了中文。

总结:需要自己去下载含有中文的字体:比如:simsun.ttf#放在指定的文件目录下。

到此这篇关于python设置中文界面实例方法的文章就介绍到这了,更多相关如何实现python设置中文界面内容请搜索自学php网以前的文章或继续浏览下面的相关文章希望大家以后多多支持自学php网!

以上就是关于python设置中文界面实例方法全部内容,感谢大家支持自学php网。

查看更多关于python设置中文界面实例方法的详细内容...

  阅读:39次