python設(shè)置中文界面實例方法
下面,小編將通過一組實例演示,讓大家更直觀,更清楚明白的了解要設(shè)置中文這一內(nèi)容的操作步驟。
首先展示實例代碼:
import pygamefrom pygame.locals import * def main():pygame.init()screen = pygame.display.set_mode((1000, 450)) #窗口的大小pygame.display.set_caption(’pygame程序的界面的中文設(shè)置’) #窗口標(biāo)題,中文不需要特別的設(shè)置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) #顯示中文的設(shè)置和字體,及路徑text = font.render('Hello 我愛你', 1, (10, 10, 10)) textpos = text.get_rect()textpos.center = background.get_rect().centerbackground.blit(text, textpos)screen.blit(background, (0, 0))pygame.display.flip()while 1:for event in pygame.event.get():if event.type == QUIT:returnscreen.blit(background, (0, 0))pygame.display.flip()if __name__ == ’__main__’: main()
運行效果展示:
注意:hello后面是亂碼,中文內(nèi)容“我愛你”并沒有顯示。
修改后的代碼展示:
import pygamefrom pygame.locals import * def main():pygame.init()screen = pygame.display.set_mode((1000, 450)) #窗口的大小pygame.display.set_caption(’pygame程序的界面的中文設(shè)置’) #窗口標(biāo)題,中文不需要特別的設(shè)置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) #顯示中文的設(shè)置和字體,及路徑text = font.render('Hello 我愛你', 1, (10, 10, 10)) textpos = text.get_rect()textpos.center = background.get_rect().centerbackground.blit(text, textpos)screen.blit(background, (0, 0))pygame.display.flip()while 1:for event in pygame.event.get():if event.type == QUIT:returnscreen.blit(background, (0, 0))pygame.display.flip()if __name__ == ’__main__’: main()
運行效果展示:
從上面可以看出,已經(jīng)顯示了中文。
總結(jié):需要自己去下載含有中文的字體:比如:simsun.ttf#放在指定的文件目錄下。
到此這篇關(guān)于python設(shè)置中文界面實例方法的文章就介紹到這了,更多相關(guān)如何實現(xiàn)python設(shè)置中文界面內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
