微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何提示用户使用python3打开文件? pygame

如何解决如何提示用户使用python3打开文件? pygame

我正在制作游戏,我希望用户能够单击一个按钮来打开文件管理器,并要求他们打开游戏保存文件。有没有模块/如何将其放入游戏中?如果没有良好的GUI方法,我将如何制作文本输入(不使用终端)?谢谢!

解决方法

Pygame是一个低级库,因此它没有您想要的内置对话框。您可以创建这样的模块,但是使用tkinter模块是更快的,该模块随Python分发,并提供TK GUI库的接口。我建议阅读文档,但这是一个可以弹出文件选择对话框,然后返回所选路径的函数:

def prompt_file():
    """Create a Tk file dialog and cleanup when finished"""
    top = tkinter.Tk()
    top.withdraw()  # hide window
    file_name = tkinter.filedialog.askopenfilename(parent=top)
    top.destroy()
    return file_name

这是一个包含此功能的小示例,按空格键将弹出对话框:

import tkinter
import tkinter.filedialog
import pygame

WIDTH = 640
HEIGHT = 480
FPS = 30

def prompt_file():
    """Create a Tk file dialog and cleanup when finished"""
    top = tkinter.Tk()
    top.withdraw()  # hide window
    file_name = tkinter.filedialog.askopenfilename(parent=top)
    top.destroy()
    return file_name

pygame.init()
window = pygame.display.set_mode((WIDTH,HEIGHT))
clock = pygame.time.Clock()

f = "<No File Selected>"
frames = 0
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_SPACE:
                f = prompt_file()

    # draw surface - fill background
    window.fill(pygame.color.Color("grey"))
    ## update title to show filename
    pygame.display.set_caption(f"Frames: {frames:10},File: {f}")
    # show surface
    pygame.display.update()
    # limit frames
    clock.tick(FPS)
    frames += 1
pygame.quit()

注意:如帧计数器所示,这将暂停游戏循环,但由于窗口管理器正在处理事件,因此这不成问题。 我不确定为什么需要显式import tkinter.filedialog,但是如果不需要,我会得到AttributeError

对于pygame中的字符串输入,您可能希望本机执行,在这种情况下,您将处理KEYUP个字母键事件来构建字符串,并可能在用户按下 Enter时结束或您自己绘制的按钮。您可以沿tk路径继续,在这种情况下,您将需要使用类似tkinter.simpledialog.askstring(…)

,

可以使用pygame_gui模块进行文件对话(尽管不使用本机文件浏览器)。

只需创建一个UIFileDialog的实例,并在用户点击“确定”时抓取路径:

file_selection = UIFileDialog(rect=Rect(0,300,300),manager=manager,initial_file_path='C:\\')
if event.ui_element == file_selection.ok_button:
    file_path = file_selection.current_file_path

如果要允许选择将allow_picking_directories设置为True的目录,但是请注意,该目录不允许选择initial_file_path

file_selection = UIFileDialog(rect=Rect(0,allow_picking_directories=True)

以下是一个简单程序中的上述代码,该代码使您可以在单击按钮时选择文件:

#!/usr/bin/env python
import pygame
import pygame_gui
from pygame_gui.windows.ui_file_dialog import UIFileDialog
from pygame_gui.elements.ui_button import UIButton
from pygame.rect import Rect

pygame.init()

window_surface = pygame.display.set_mode((800,600))

background = pygame.Surface((800,600))
background.fill(pygame.Color('#000000'))

manager = pygame_gui.UIManager((800,600))
clock = pygame.time.Clock()

file_selection_button = UIButton(relative_rect=Rect(350,250,100,100),text='Select File')

while 1:
    time_delta = clock.tick(60) / 1000.0

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            quit()

        if event.type == pygame.USEREVENT:
            if event.user_type == pygame_gui.UI_BUTTON_PRESSED:
                if event.ui_element == file_selection_button:
                    file_selection = UIFileDialog(rect=Rect(0,allow_picking_directories=True)

                if event.ui_element == file_selection.ok_button:
                    print(file_selection.current_file_path)

        manager.process_events(event)

    manager.update(time_delta)
    window_surface.blit(background,(0,0))
    manager.draw_ui(window_surface)

    pygame.display.update()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。