Tkinter Button muss in bestimmten Abstand gedrückt werden

Fragen zu Tkinter.
Antworten
zp3x_
User
Beiträge: 2
Registriert: Sonntag 19. Dezember 2021, 08:35

Hallo ich möchte in tkinter einen Button machen
Den man in 2 Sekunden drücken muss ansonsten soll

Code: Alles auswählen

def reset():
    print("du warst nicht schnell genug")
ausgegeben werden

Mein Code bis jetzt (der Button der in der bestimmten Zeit gedrückt werden soll heißt hitButton):

Code: Alles auswählen

from tkinter import *
import random




xr = 40
yr = 40

global hitButton

def restartGame():
    root.destroy()
    Main()

def noCommand():
    print("no command executed")


def failEvent():
    global hitButton
    print("failed")
    OverLabel = Label(image=GameOverIMG)
    OverLabel.place(x=200, y=80)
    print("test")
    hitButton.place(x=232323, y=232323)
    bRestart = Button(root, text="Restart", fg="WHITE", bg="BLACK", command=restartGame, width=5, height=3)
    bRestart.place(x=200, y=700)

    


def ButtonHitEvent():
    global xr
    global yr

    xr = random.randint(40, 950)
    yr = random.randint(40, 750)

    hitButton.place(x=xr, y=yr)

def Main():
    global hitButton
    global ButtonImg
    global ClearIMG
    global clearLabelGameOver
    global ButtonWindow
    global hitButton
    global GameOverIMG
    global root

    root = Tk()
    root.geometry("1000x800")

    ButtonImg = PhotoImage(file="C:/Users/jakob/AppData/Local/Programs/Python/Python39/Scripts/Scripts/Python/Tkinter/graphics/ButtonClick.png")
    GameOverIMG = PhotoImage(file="C:/Users/jakob/AppData/Local/Programs/Python/Python39/Scripts/Scripts/Python/Tkinter/graphics/GameOver.png")
    ClearIMG = PhotoImage(file="C:/Users/jakob/AppData/Local/Programs/Python/Python39/Scripts/Scripts/Python/Tkinter/graphics/clear.png")

    clearLabelGameOver = Label(root, image=ClearIMG)
    clearLabelGameOver.place(x=300, y=200)

    ButtonWindow = Button(root, text=" ", width=300, height=300, command=failEvent, background="BLACK", foreground="BLACK").pack()

    hitButton = Button(root, image=ButtonImg, command=ButtonHitEvent, fg="BLACK", bg="BLACK")
    hitButton.place(x=40, y=40)

    root.mainloop()
if __name__ == '__main__':
    Main()
Sirius3
User
Beiträge: 17754
Registriert: Sonntag 21. Oktober 2012, 17:20

Vergiss gleich wieder, dass es `global` gibt. Für GUI-Programme braucht man zwangsläufig Klassendefinitionen.
*-Importe sind schlecht, weil man nicht kontrollieren kann, welche Namen da in den eigenen Namensraum geschaufelt werden.
Variablennamen werden wie Funktionen komplett klein geschieben.
Im ganzen Programmablauf darf es auch nur ein Tk-Exemplar geben, das machst Du mit Deiner rekursiven Restart-Funktion kaputt. Rekursion ist auch kein sinnvoller Weg, etwas von Vorne starten zu lassen.
Und wenn Du das Programm sauber neu aufgesetzt hast, dann ist `after` die Methode, die Du suchst.

Das ganze könnte zum Beispiel so aussehen:

Code: Alles auswählen

import tkinter as tk
import random
from pathlib import Path

IMAGE_PATH = Path(__file__).parent / "graphics"

class Game(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.geometry("1000x800")
        self.button_image = tk.PhotoImage(file=str(IMAGE_PATH / "ButtonClick.png"))
        self.game_over_image = tk.PhotoImage(file=str(IMAGE_PATH / "GameOver.png"))
        self.clear_image = tk.PhotoImage(file=str(IMAGE_PATH / "clear.png"))
        tk.Label(self, image=self.clear_image).place(x=300, y=200)
        tk.Button(self, text=" ", width=300, height=300, command=self.fail,
            background="BLACK", foreground="BLACK").pack()
        self.hit_button = tk.Button(self, image=self.button_image, command=self.hit,
            fg="BLACK", bg="BLACK")
        self.gameover_label = tk.Label(self, image=self.game_over_image)
        self.restart_button = tk.Button(self, text="Restart", fg="WHITE", bg="BLACK",
            command=self.restart, width=5, height=3)
        self.hit()

    def hit(self):
        xr = random.randint(40, 950)
        yr = random.randint(40, 750)
        self.hit_button.place(x=xr, y=yr)

    def fail(self):
        self.hit_button.place_forget()
        self.gameover_label.place(x=200, y=80)
        self.restart_button.place(x=200, y=700)

    def restart(self):
        self.gameover_label.place_forget()
        self.restart_button.place_forget()
        self.hit()

def main():
    root = Game()
    root.mainloop()

if __name__ == '__main__':
    main()
Antworten