Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

[Python3 SDL2] Podstawowe elementy GUI czyli Buttony

Ostatnio zmodyfikowano 2025-07-21 16:17
Autor Wiadomość
tBane
Temat założony przez niniejszego użytkownika
[Python3 SDL2] Podstawowe elementy GUI czyli Buttony
» 2025-07-20 19:53:59
Cześć. Uczę się od niedawna  Python3 i SDL2. Chciałbym stworzyć ładny przycisk z tekstem. Jak się za to najlepeij zabrać ?
Poniżej zamieszczam ogólny kod przycisku (bez tekstu z ramką), który działa.

Button.py
Python

import sdl2
import sdl2.ext
import numpy as np

class Button:
   
x : int
    y : int
    wdt : int
    hgh : int
    r : int
    g : int
    b : int

    def __init__(self, x : int, y : int, width: int, height: int):
       
self.x = x
        self.y = y
        self.wdt = width
        self.hgh = height

        self.set_color(64, 64, 64)

   
def set_color(self, r : int, g : int, b : int):
       
self.r = r
        self.g = g
        self.b = b

    def cursorHover(self, mouse_x : int, mouse_y : int) -> bool:
       
if( mouse_x >= self.x and mouse_y >= self.y and mouse_x <= self.x + self.wdt and mouse_y <= self.y + self.hgh):
           
self.set_color(128, 128, 128)
           
return True
        else
:
           
self.set_color(64, 64, 64)
           
return False;

   
def draw(self, renderer):

       
# border
       
renderer.color = sdl2.ext.Color(32, 32, 32)
       
rect = sdl2.SDL_Rect(self.x, self.y, self.wdt, self.hgh)
       
renderer.fill(rect)

       
# main button area
       
margin = 4
       
renderer.color = sdl2.ext.Color(self.r, self.g, self.b)
       
rect = sdl2.SDL_Rect(self.x + margin, self.y + margin, self.wdt - 2*margin, self.hgh-2*margin)
       
renderer.fill(rect)
P-182790
tBane
Temat założony przez niniejszego użytkownika
» 2025-07-20 20:41:59
Mam na chwilę obecną taki kod i efekt:


Python

import sdl2
import sdl2.ext
import numpy as np

class Button:
   
x : int
    y : int
    wdt : int
    hgh : int
    r : int
    g : int
    b : int

    hide_tex : sdl2.SDL_Texture = None
   
high_tex : sdl2.SDL_Texture = None
   
curr_tex : sdl2.SDL_Texture = None

    def
__init__(self, renderer, x : int, y : int, width: int, height: int, hide_texture : str, high_texture : str):
       
self.x = x
        self.y = y
        self.wdt = width
        self.hgh = height

        self.set_color(64, 64, 64)
       
self.set_textures(renderer, hide_texture, high_texture)
       

   
def destroy(self):

       
if hasattr(self, "hide_tex") and self.hide_tex:
           
sdl2.SDL_DestroyTexture(self.hide_tex)
           
self.hide_tex = None

        if
hasattr(self, "high_tex") and self.high_tex:
           
sdl2.SDL_DestroyTexture(self.high_tex)
           
self.high_tex = None

        if
hasattr(self, "curr_tex") and self.curr_tex:
           
sdl2.SDL_DestroyTexture(self.curr_tex)
           
self.curr_tex = None

    def
set_color(self, r : int, g : int, b : int):
       
self.r = r
        self.g = g
        self.b = b

    def set_textures(self, renderer, hide_texture : str, high_texture : str):

       
surface_hide = sdl2.ext.load_image(hide_texture)
       
self.hide_tex = sdl2.SDL_CreateTextureFromSurface(renderer.sdlrenderer, surface_hide)
       
sdl2.SDL_FreeSurface(surface_hide)

       
surface_hgh = sdl2.ext.load_image(high_texture)
       
self.high_tex = sdl2.SDL_CreateTextureFromSurface(renderer.sdlrenderer, surface_hgh)
       
sdl2.SDL_FreeSurface(surface_hgh)

       
self.curr_tex = self.hide_tex

    def cursorHover(self, mouse_x : int, mouse_y : int) -> bool:
       
if( mouse_x >= self.x and mouse_y >= self.y and mouse_x <= self.x + self.wdt and mouse_y <= self.y + self.hgh):
           
self.set_color(80, 80, 80)
           
self.curr_tex = self.hide_tex
            return True
        else
:
           
self.set_color(64, 64, 64)
           
self.curr_tex = self.high_tex
            return False;

   
def draw(self, renderer):

       
renderer.color = sdl2.ext.Color(32, 32, 32)
       
rect = sdl2.SDL_Rect(self.x, self.y, self.wdt, self.hgh)
       
renderer.fill(rect)

       
margin = 4
       
renderer.color = sdl2.ext.Color(self.r, self.g, self.b)
       
rect = sdl2.SDL_Rect(self.x + margin, self.y + margin, self.wdt - 2*margin, self.hgh-2*margin)
       
renderer.fill(rect)

       
sdl2.SDL_RenderCopy(renderer.sdlrenderer, self.curr_tex, None, rect)


       
P-182791
tBane
Temat założony przez niniejszego użytkownika
» 2025-07-20 21:49:32
Jednak zrobię w tym programie przycisk z grafiką, ale żeby nie zostawić niedokończonego tematu napisze też przycisk z tekstem i wrzucę kod
P-182792
tBane
Temat założony przez niniejszego użytkownika
» 2025-07-21 16:17:29
Udało się. Przycisk po prawej u góry z napisem "hello". Kod zamieszczam poniżej :-)



Python

class ButtonWithText:
   
x : int
    y : int
    wdt : int
    hgh : int
    r : int
    g : int
    b : int
    txt : str
    font_manager : sdl2.ext.FontManager

    def __init__(self, renderer, text : str, x : int, y : int, width: int, height: int):
       
       
self.txt = text
        self.font_manager = sdl2.ext.FontManager("C:/Windows/Fonts/arial.ttf", 16);

       
self.x = x
        self.y = y
        self.wdt = width
        self.hgh = height

        self.set_color(64, 64, 64)


   
def set_color(self, r : int, g : int, b : int):
       
self.r = r
        self.g = g
        self.b = b

    def set_text(self, text : str):
       
self.txt = text

    def cursorHover(self, mouse_x : int, mouse_y : int) -> bool:
       
if( mouse_x >= self.x and mouse_y >= self.y and mouse_x <= self.x + self.wdt and mouse_y <= self.y + self.hgh):
           
self.set_color(80, 80, 80)
           
return True
        else
:
           
self.set_color(64, 64, 64)
           
return False;

   
def draw(self, renderer):

       
renderer.color = sdl2.ext.Color(32, 32, 32)
       
outer_rect = sdl2.SDL_Rect(self.x, self.y, self.wdt, self.hgh)
       
renderer.fill(outer_rect)

       
margin = 4
       
renderer.color = sdl2.ext.Color(self.r, self.g, self.b)
       
inner_rect = sdl2.SDL_Rect(self.x + margin, self.y + margin, self.wdt - 2*margin, self.hgh-2*margin)
       
renderer.fill(inner_rect)

       
surface = self.font_manager.render(self.txt, color=sdl2.ext.Color(255, 255, 255))
       
texture = sdl2.SDL_CreateTextureFromSurface(renderer.sdlrenderer, surface)
       
w = sdl2.c_int()
       
h = sdl2.c_int()
       
sdl2.SDL_QueryTexture(texture, None, None, w, h)

       
dstrect = sdl2.SDL_Rect(
           
self.x + (self.wdt - w.value) // 2,
           
self.y + (self.hgh - h.value) // 2,
           
w.value, h.value
        )

       
sdl2.SDL_RenderCopy(renderer.sdlrenderer, texture, None, dstrect)
       
sdl2.SDL_DestroyTexture(texture)
       
sdl2.SDL_FreeSurface(surface)
P-182798
« 1 »
  Strona 1 z 1