Hogyan készítsünk több gombot ciklusban, (loop) a Pygame GUI -ban.
Így javaslom Windows 10 -en:
import pygame as pg
import pygame_gui
"""pygame-ce 2.5.5 (SDL 2.32.6, Python 3.12.8)"""
class JsonIndito:
def __init__(self):
pg.init()
self.SZELES = 800
self.MAGAS = 600
pg.display.set_caption('A json alkalmazása.')
self.ablak = pg.display.set_mode((self.SZELES, self.MAGAS))
self.hatter = pg.Surface((self.SZELES, self.MAGAS))
self.hatter.fill(pg.Color('darkgreen'))
self.fonok = pygame_gui.UIManager(
(self.SZELES, self.MAGAS),
theme_path="gyorsindítás.json")
self.gomb_nevek = ['gomb1', 'gomb2', 'gomb3', 'gomb4', 'gomb5', ]
self.gombok = []
for i, nev in enumerate(self.gomb_nevek):
gomb = pygame_gui.elements.UIButton(
relative_rect=pg.Rect((5 + i * 100, self.MAGAS-50), (100, 50)),
text=self.gomb_nevek[i],
manager=self.fonok)
self.gombok.append(gomb)
self.ido_mero = pg.time.Clock()
self.futas = True
while self.futas:
delta_ido = self.ido_mero.tick(60 ) /1000.0
for event in pg.event.get():
if event.type == pg.QUIT:
self.futas = False
if event.type == pygame_gui.UI_BUTTON_PRESSED:
if event.ui_element == self.gombok[0]:
print('Monoton üget a süppegő homokon a tétova teve tova.')
self.fonok.process_events(event)
self.fonok.update(delta_ido)
self.ablak.blit(self.hatter, (0, 0))
self.fonok.draw_ui(self.ablak)
pg.display.update()
if __name__ == '__main__':
indito = JsonIndito()
A hozzátartozó: gyorsindítás.json fájl.
{
"button":
{
"colours":
{
"normal_border": "black",
"normal_bg": "#e2f906",
"normal_text": "black"
},
"misc":
{
"shape": "rounded_rectangle",
"shape_corner_radius": "10",
"border_width": "2"
}
}
}
Nem írunk Magyarul kódot? De!
Ebben én jobb vagyok mint az AI -k!
Főleg ha tanulmányozom az alábbi oldalt:
https://pygame-gui.readthedocs.io/en/latest/quick_start.html
Nincsenek megjegyzések:
Megjegyzés küldése