用 Python 在屏幕上显示文本的简单方法? [英] Simple way to display text on screen in Python?

查看:170
本文介绍了用 Python 在屏幕上显示文本的简单方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在谷歌搜索并查看 StackOverflow,但没有运气......

I've been googling and looking through StackOverflow but no luck...

我想要为我正在开发的艺术项目在屏幕(而不是控制台)上显示文本.该字符串将在全屏中显示——没有菜单栏的边到边窗口"等.该字符串将快速更新(想象一串随机字母以计算机允许的速度显示和重绘).字体和颜色的选择(逐个字母)将是很好的奖励.

What I would like is to display text to the screen (not console) for an art project I am developing. The string will be displayed in a full screen--edge to edge "window" with no menubar, etc. The string will update rapidly (imagine a string of random letters displaying and redrawing as fast as the computer will allow). Choice of font and color (on a letter-by-letter basis) would be great bonuses.

字符串将在屏幕上居中(不滚动).

The string will be centered on the screen (not scrolling).

我是编程新手.在屏幕上显示文本似乎比我预期的要复杂得多.:)

I'm new-ish to programming. Displaying text to the screen seems much more involved than I expected. :)

我遇到过使用 pygame 的解决方案,但示例代码不起作用.下面是一个典型的例子:

I've come across solutions using pygame but the sample code doesn't work. Here's a typical example:

https://www.daniweb.com/programming/software-development/code/244474/pygame-text-python

我在 Mac 上看到的是一个没有文本的窗口打开,代码无限期地继续运行.我遇到的其他代码和其他库的类似经历.

What I see on my Mac is a window opening with no text, with the code continuing to run, indefinitely. Similar experiences with other code and other libraries I've encountered.

pygame 是满足我需求的最佳选择(易于使用 Python 编程,速度快)还是有其他更适合我的库?

Is pygame my best choice (ease of programming in Python, high speed) for my needs or are there other libraries better suited to what I am after?

如果 pygame 是正确的方向,谁能建议示例代码让我继续前进?

If pygame is the right direction can anyone suggest sample code that will get me going?

谢谢,

--达林

推荐答案

这是一个 pygame 解决方案.只需将随机字母和颜色传递给 Font.render 然后 blit 返回的表面.为了使其居中,我调用 txt 表面的 get_rect 方法并将 screen_rect 的中心作为 center争论.请注意,您不能为每个字母选择不同的颜色.要做到这一点,您可能需要渲染几个一个字母"的表面,然后将它们组合起来.

Here's a pygame solution. Just pass the random letters and color to Font.render and then blit the returned surface. To center it I call the get_rect method of the txt surface and pass the center of the screen_rect as the center argument. Note that you can't choose a different color for each letter. To do that you would probably have to render several "one letter" surfaces and then combine them.

癫痫警告 - 文本变化非常快(每帧 (30 fps)),我不确定这是否会导致癫痫发作,所以我添加了一个计时器来更新文本仅每 10 帧.如果您想提高更新速度,请小心.

Epilepsy warning - The text was getting changed really quickly (each frame (30 fps)) and I wasn't sure if that could cause epileptic seizures, so I added a timer to update the text only every 10 frames. Be careful if you want to increase the update speed.

import sys
from random import choice, randrange
from string import ascii_letters

import pygame as pg


def random_letters(n):
    """Pick n random letters."""
    return ''.join(choice(ascii_letters) for _ in range(n))


def main():
    info = pg.display.Info()
    screen = pg.display.set_mode((info.current_w, info.current_h), pg.FULLSCREEN)
    screen_rect = screen.get_rect()
    font = pg.font.Font(None, 45)
    clock = pg.time.Clock()
    color = (randrange(256), randrange(256), randrange(256))
    txt = font.render(random_letters(randrange(5, 21)), True, color)
    timer = 10
    done = False

    while not done:
        for event in pg.event.get():
            if event.type == pg.KEYDOWN:
                if event.key == pg.K_ESCAPE:
                    done = True

        timer -= 1
        # Update the text surface and color every 10 frames.
        if timer <= 0:
            timer = 10
            color = (randrange(256), randrange(256), randrange(256))
            txt = font.render(random_letters(randrange(5, 21)), True, color)

        screen.fill((30, 30, 30))
        screen.blit(txt, txt.get_rect(center=screen_rect.center))

        pg.display.flip()
        clock.tick(30)


if __name__ == '__main__':
    pg.init()
    main()
    pg.quit()
    sys.exit()

这篇关于用 Python 在屏幕上显示文本的简单方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆