Question

I am working on a progress bar that keeps track of a certain function in pygame.

The following code causes a loop that I must force quit. And I can not figure our my error. Any help would be great.

from Tkinter import *
import ttk
import sys
import pygame

myGui = Tk()

myGui.title("Progress Bar")
myGui.geometry("400x200+200+300")

value_progress = StringVar()

pygame.mixer.init()
pygame.mixer.music.load("/home/david/Documents/aaa.mp3")
pygame.mixer.music.play()


def position():
    global value_progress
    while pygame.mixer.music.get_busy() == True: 
        value_progress.set(float(pygame.mixer.music.get_pos()))


b = Button(myGui, text="Start", )
b.pack()


p = ttk.Progressbar(myGui, variable=value_progress,
                    mode='determinate', length=350,
                    maximum= 512920)
p.pack()

I call the function from the shell. and then it stalls and does not come out of it.

This is only the progress bar portion of my work. However, it causes the program to crash every time.

Was it helpful?

Solution

Do not loop. Instead try following code:

def position():
    global value_progress
    if pygame.mixer.music.get_busy():
        value_progress.set(float(pygame.mixer.music.get_pos()))
        myGui.after(100, position)

Tk.after(ms, f) call f after specified ms millisecond.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top