在pygame中使用文字输入 [英] Using text inputs in pygame

查看:900
本文介绍了在pygame中使用文字输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import tkinter as tk    
import pygame

pygame.init()
ss = width, height = 1024, 600
screen = pygame.display.set_mode(ss)
tkinput_1 = True    

while True:

    event = pygame.event.poll()
    keys = pygame.key.get_pressed()

    if event.type == pygame.QUIT:
        pygame.quit()
        sys.exit()

    screen.fill((0,0,0))

    if tkinput_1 == True:
         tkinput_root1 = tk.Tk()
         tkinput_root1.geometry("200x200")
         tkinput_root1.title("How many teams?")
         tkinput_input1 = tk.Entry(tkinput_root1)
         tkinput_1 = False

    pygame.display.update()

tkinput_root1.mainloop()

这只是我在pygame中为tkinter文本输入框提供了一个镜头.我相当确定它不会正常运行,但决定无论如何我都会尝试,因为我没有任何损失. 退出pygame之前,不会显示tkinter屏幕. 因此,我并不是要问是否有人知道如何修复代码,而是要问是否有人知道在pygame中创建文本框的最简单方法.我知道有一个textinput类模块可以导入.如果您认为这是最简单的方法,那么可以带我逐步了解一下.如果不能,请让我知道您认为最简单的方法是什么.这是一个小程序,所以我想避免为一个简单的文本框写很多代码.让我知道你们的想法. 提前非常感谢.

This was just me giving the tkinter text input box a shot in pygame. I was fairly sure it wouldn't work out properly but decided I'd try it anyway cause I had nothing to lose. The tkinter screen does not show up till you've exited pygame. So, I'm not asking if anyone knows how to fix the code, but instead if anyone knows the simplest way to create a text box in pygame. I know there is a textinput class module that can be imported. If you believe that is the easiest way to do it then can you walk me through it. If not could you please let me know what you think the easiest way is. It's a small program so I want to avoid a lot of heavy lines of code for a simple text box. Let me know what you guys think. Thanks a lot in advance.

推荐答案

我尝试使用Tk,但使用的问题是,当Tk窗口弹出时,它会停止整个pygame程序,只是效果不好

I've tryed using Tk but the thing about using is it stops the whole pygame program when the Tk window pops up and its just not good

这是我使用的 Pygame InputBox 它不是最漂亮的,但是下载它就可以了它及其真正易于使用的

this is what i use Pygame InputBox its not the prettiest but it works great just download it and its really easy to use

只是import inputbox

然后执行以下操作:

inp = int(inputbox.ask(screen, 'Message')) #inp will equal whatever the input is

这与raw_input非常相似,但对于pygame

this is pretty much like raw_input but for pygame

它不是最美观的,但是我不确定如果您四处搜索,也许会找到一个更好的

its not the most aesthetically pleasing but im sure if you search around you can find maybe a nicer one

这篇关于在pygame中使用文字输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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