Seite 1 von 1

Wie mache ich einen Button ???

Verfasst: Montag 16. März 2015, 14:16
von N4SONIC
Kann mir jemand ein beispiel schicken wie man ein Button erstellt der wenn man ihn anklickt eine Funktion ausgibt ? :(

Re: Wie mache ich einen Button ???

Verfasst: Montag 16. März 2015, 14:51
von BlackJack
@N4SONIC: Womit denn? Das hier ist ja das Unterforum für alle GUI-Rahmenwerke für die es kein spezielles eigenes Unterforum gibt, also solltest Du vielleicht erwähnen welches Rahmenwerk oder welche Bibliothek Du verwendest.

Und die nächste Frage wäre dann wo das konkrete Problem liegt. Wie sieht Dein Code aus? Was funktioniert daran nicht?

Re: Wie mache ich einen Button ???

Verfasst: Montag 16. März 2015, 14:55
von N4SONIC
Ich benutze Python(Pygame)

Ich habe kein Code ich weiß einfach nicht wie man so ein Button macht :/

Re: Wie mache ich einen Button ???

Verfasst: Montag 16. März 2015, 14:58
von BlackJack
@N4SONIC: Naja man muss halt prüfen ob der Benutzer die Maustaste drückt und falls ja ob sich der Mauszeiger innerhalb des Bereichs befindet der einen Button darstellen soll. Falls ja, macht man das was der Button halt so bewirken soll.

Re: Wie mache ich einen Button ???

Verfasst: Montag 16. März 2015, 15:18
von N4SONIC
@BlackJack: Ich habe es hinbekommen danke :)

Jetzt habe ich eine andere frage wie bekomme ich es hin das der Button das "HELLO" nur einmal ausgibt wenn ich ihn aktiviere ??? :o

Code: Alles auswählen

import pygame

pygame.init()

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

pygame.display.set_caption("Window")

gameExit = False

green = (0,200,0)

class Button:
    def __init__(self,x,y,h,w):
        self.x = x
        self.y = y
        self.h = h
        self.w = w

    def update(self,window):
        mouse = pygame.mouse.get_pos()
        click = pygame.mouse.get_pressed()

        if click[0] == True:
            print("HELLO")

        if 400+100 > mouse[0] > 400 and 300+50 > mouse[1] > 300:
            pygame.draw.rect(window, (100,150,50), (self.x, self.y, self.h, self.w))
        else:
            pygame.draw.rect(window, (125,100,50), (self.x, self.y, self.h, self.w))

button1 = Button(400,300,100,50)

while not gameExit:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            gameExit = True

    window.fill((255,255,255))

    button1.update(window)

    pygame.display.update()

pygame.quit()
quit()

Re: Wie mache ich einen Button ???

Verfasst: Montag 16. März 2015, 16:22
von Hyperion
Ich wusste doch, dass mir die Frage bekannt vor kam: http://www.python-forum.de/viewtopic.php?f=4&t=35712 :roll:

Re: Wie mache ich einen Button ???

Verfasst: Montag 16. März 2015, 19:42
von N4SONIC
Kann mir nicht jemand einfach ein example machen ? :/

Re: Wie mache ich einen Button ???

Verfasst: Dienstag 17. März 2015, 14:31
von N4SONIC
Ich habe es hinbekommen :)

Ist der Code so gut oder kann man noch was verbessern ? :)

Code: Alles auswählen

import pygame

pygame.init()

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

pygame.display.set_caption("Window")

gameExit = False

green = (0,200,0)

def message():
    print("Button Click!!!")

class Button:
    def __init__(self,x,y,h,w,f):
        self.x = x
        self.y = y
        self.h = h
        self.w = w
        self.f = f

    def mouse_click(self):
        click = pygame.mouse.get_pressed()
        mouse = pygame.mouse.get_pos()

        if 400+100 > mouse[0] > 400 and 300+50 > mouse[1] > 300:
            if click[0] == 1:
                self.f()

    def mouse_effect(self):
        mouse = pygame.mouse.get_pos()

        if 400+100 > mouse[0] > 400 and 300+50 > mouse[1] > 300:
            pygame.draw.rect(window, (100,150,50), (self.x, self.y, self.h, self.w))
        else:
            pygame.draw.rect(window, (125,100,50), (self.x, self.y, self.h, self.w))

    def update(self):
        self.mouse_effect()


button1 = Button(400,300,100,50,message)

while not gameExit:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            gameExit = True

        if event.type == pygame.MOUSEBUTTONDOWN:
            button1.mouse_click()

    window.fill((255,255,255))

    button1.update()

    pygame.display.update()

pygame.quit()
quit()