首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SGC GUI和Pygame Widget的实现

SGC GUI和Pygame Widget的实现
EN

Stack Overflow用户
提问于 2019-11-24 21:27:56
回答 1查看 118关注 0票数 0

嗨,我正在尝试用Pygame编写一个简单的应用程序。我进行了各种搜索,发现获取用户输入的最好方法是使用第三方GUI。

我已经为这个目的找到了简单的游戏代码。下面,你可以找到我的基本代码,它在脚本的相同路径中查找图像,并在屏幕上按顺序替换它们。

但我没有使用这种应用程序的经验。我试图从SGC的文档中理解:https://github.com/codetricity/sgc/blob/master/example/test.py

这对我来说不是一件容易的事。我可以开发到这一步,我的代码正在运行。但是我不能理解按钮实现部分。

你能帮我在开始的时候实现一个"Scale Widget“来获取一系列整数之间的用户输入吗?另外,一个“按钮小部件”,通过开始屏幕和开始我的主要代码,我将与你分享。

耽误您时间,实在对不起

代码语言:javascript
复制
import glob
import time
import numpy as np
import timeit
import pygame
import sgc 
from sgc.locals import *

start = timeit.default_timer()

maxnote = 10
maxduration = 10

pygame.init()

white = (255, 255, 255) 

path = r'C:\Path'

mylistname = [f for f in sorted(glob.glob("*.png"))]
mylistpath = [f for f in sorted(glob.glob(path + "/*.png"))]

for i in range(len(mylistname)):

    mylistname[i] = mylistname[i].replace(".png", "")
    mylistname[i] = mylistname[i].replace("h", ".")
    mylistname[i] = float(mylistname[i])

imgname = []

for i in range(len(mylistname)):
    imgname.append(str("img" + str(mylistname[i])))

imglist = []

for i in range(len(mylistpath)):

    name = str(imgname[i])
    name = pygame.image.load(mylistpath[i]) 
    imglist.append(name)

current_image = 0

display_surface = pygame.display.set_mode((400, 400)) 

while (timeit.default_timer() - start < maxduration) | (current_image < maxnote):
#for imj in range(len(imglist)+1):

    print(str(current_image) + "s")

    if current_image < len(imglist):
        print(str(current_image) + "0")

        while True:

            print(str(current_image) + "p")
            display_surface.fill(white)
            display_rect = display_surface.get_rect()    
            image_rect = imglist[current_image].get_rect()     
            image_rect.center = display_rect.center
            display_surface.blit(imglist[current_image],image_rect)
            pygame.display.update()
            pygame.display.flip()

            time.sleep(5)

            current_image = current_image + 1
            print(str(current_image) + "n")

            break
    else:

        font = pygame.font.Font('freesansbold.ttf', 32) 
        text = font.render('GeeksForGeeks', True, (0, 255, 0), (0, 0, 128))
        textRect = text.get_rect()
        textRect.center = display_rect.center
        display_surface.blit(text, textRect)
        pygame.display.update()
        pygame.display.flip()
        time.sleep(5)

        pygame.display.quit()

print("the end")
EN

回答 1

Stack Overflow用户

发布于 2019-11-25 22:25:30

使用您的代码,我添加了SGC按钮,它显示在图像上,当它被单击时,它会在控制台中显示文本。

我有两个问题:

  • SGC很旧,只能与Python2一起使用。对于Python3,它需要相对导入。但是后来它可能需要其他的图形用户界面阻塞循环来检查按键/鼠标事件,更新窗口小部件,当按钮被点击时运行函数,等等。sleep在所有的changes.
  • time.sleep()框架(tkinter,PyQt,wxpython,等等)中都有这个问题。在PyGame循环中,它必须一直运行以检查和更新widgets和其他元素。我使用clock检查是否是时候更改图像了。这种方式的循环可以一直工作,当鼠标移动到按钮上并单击它时,它可以更新按钮。

在Python 2.7、Linux Mint 19.2上测试

代码语言:javascript
复制
import glob
import pygame
import time

import sgc 
from sgc.locals import *

# --- constants --- (UPPER_CASE)

WHITE = (255, 255, 255) 

MAXNOTE = 10
MAXDURATION = 10

PATH = r'C:\Path'

# --- functions --- (lower_case_names)

def on_click_button():
    print('on_click_button')

# ---  main ---

filenames = sorted(glob.glob(PATH + "/*.png"))

print('len:', len(filenames))

names = []
images = []
for item in filenames:
    names.append("img" + item.replace(".png", "").replace("h", "."))
    images.append(pygame.image.load(item))

current_image = 0

# --- 

pygame.init()

display_surface = sgc.surface.Screen((400, 400))
#display_surface = pygame.display.set_mode((400, 400)) 
display_rect = display_surface.get_rect()    

font = pygame.font.Font('freesansbold.ttf', 32) 

# add button 
btn = sgc.Button(label="Clicky", pos=(10, 10))#, label_font=font)
btn.add(0)

# assign function to button
btn.on_click = on_click_button

# ---

clock = pygame.time.Clock()

current_time = pygame.time.get_ticks()
end_time = current_time + MAXDURATION*1000
end_slide = current_time

running = True
while running and ((current_time < end_time) or (current_image < MAXNOTE)):

    ticks = clock.tick(30)

    for event in pygame.event.get():

        # send events to SGC so it can check if button was clicke
        sgc.event(event)

        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
               running = False

    current_time = pygame.time.get_ticks()

    if (end_slide <= current_time) and (current_image < len(images)):
        image = images[current_image]
        image_rect = image.get_rect()     
        image_rect.center = display_rect.center

        end_slide = current_time + 2000 # 2000ms (2s)
        current_image += 1

        display_surface.fill(WHITE)
        display_surface.blit(image, image_rect)

    # draw all widgets    
    sgc.update(ticks)

    pygame.display.flip() # doesn't need pygame.display.update() because both do the same 

# ---

display_surface.fill(WHITE)

text = font.render('GeeksForGeeks', True, (0, 255, 0), (0, 0, 128))
text_rect = text.get_rect()
text_rect.center = display_rect.center
display_surface.blit(text, text_rect)

#pygame.display.update() # no need it
pygame.display.flip()

time.sleep(5)

# --- end ---

pygame.display.quit()

print("the end")

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59018142

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档