ich bin neu hier und erstelle gerade mein erstes Python Programm.
Ich möchte eine While-Schleife mit einem Button beenden (Tkinter).
Soweit funktioniert das auch, ich beende die Schleife - jedoch hängt sich mein Programm danach auf.
Code: Alles auswählen
import tkinter as tk
from tkinter import ttk
from PIL import Image, ImageTk
import time
import sys
import tkinter.ttk as ttk
from tkinter import filedialog, simpledialog, messagebox, colorchooser
import threading
import pygame
pygame.mixer.init()
root = tk.Tk()
root.title("Metronome")
root.geometry("800x400")
root.minsize(width=600, height=300)
root.configure(bg="#517357")
image = Image.open("logo.png").resize((60, 80))
photo = ImageTk.PhotoImage(image)
active = tk.BooleanVar()
active.set(True)
def stopme():
active.set(False)
t1.join()
#root.update_idletasks()
def play_metronome():
seconds_per_metronome = 60 / int(entry1.get())
i = 0
while active.get():
pygame.mixer.music.load("C:\\Users\\Marvin Klankert\\Desktop\\Metronome\\metronome_sound.mp3")
pygame.mixer.music.play(loops=0)
text_metronome = i
labelAusgabe.config(text=text_metronome)
print(i)
i = i + 1
#labelAusgabe.delete("1.0", "end") # Löscht textfeld
#labelAusgabe.insert(tk.END, i)
time.sleep(seconds_per_metronome)
root.update_idletasks()
#print(active.get())
def start():
print(t1)
active.set(True)
t1.start()
t1 = threading.Thread(target=play_metronome)
labelAusgabe = tk.Label(root, text="", font="Calibri 32 bold", bg="#517357", height=1)
labelAusgabe.place(x=400, y=100, width=150, height=150)
buttonstop = tk.Button(root, text="Stop", width=5, height=1, fg="red", command=stopme)
buttonstop.place(x=100, y=280)
label1 = tk.Label(root, bg="#517357")
label1.pack()
label2 = tk.Label(root, text="Metronome", font="Calibri", bg="white")
label2.pack(fill="x")
label3 = tk.Label(root, bg="#517357")
label3.pack()
label4 = tk.Label(root, image=photo)
label4.place(x=700, y=50)
buttonstart = tk.Button(root, text="Start", width=15, height=10, command=start)
buttonstart.place(x=100, y=100)
button2 = tk.Button(root, text="Quit", width=9, command=root.destroy)
button2.place(x=700, y=350)
entry1 = ttk.Entry(root, width=10)
entry1.place(x=200, y=354)
label5 = tk.Label(root, text="Tempo (BPM): ", font="Calibri", bg="#517357", height=1)
label5.place(x=100, y=350)
#labelAnzeige = tk.Label(root, textvariable="")
#labelAnzeige.place(x=400, y=100, width=150, height=150)
root.mainloop()
Mit dem buttonstart springe ich in "def play_metronome():", wo sich die While Schleife befindet.
Mit buttonstop möchte ich es beenden.
Vielen Dank,
Black Dullah