gpt4 book ai didi

python - 在pygame中使用文本输入

转载 作者:太空狗 更新时间:2023-10-30 00:36:29 25 4
gpt4 key购买 nike

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 类模块。如果您认为这是最简单的方法,那么您能引导我完成它吗?如果不能,请让我知道您认为最简单的方法是什么。这是一个小程序,所以我想避免为一个简单的文本框编写大量繁重的代码。让我知道你们的想法。非常感谢。

最佳答案

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

这是我用的Pygame InputBox它不是最漂亮的,但它很好用,只需下载它,而且非常易于使用

只需导入输入框

然后做这样的事情:

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

这很像 raw_input 但对于 pygame

它不是最美观的,但我相信如果你四处搜索,你可能会找到更好的

关于python - 在pygame中使用文本输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17450683/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com