Sarah
Sarah

Reputation: 617

Tkinter stringVar() for a button text, how can I use the set text?

I'm making a music player using Tkinter as an interface, and I'm trying to change the "PLAY" button to "PAUSE" when clicked. And when I click the "PAUSE" button, it goes back to "PLAY". Below is my code:

from tkinter import *
import time, sys
from pygame import mixer

track_names = ['lady maria', 'cleric beast']
current_track = ''


def press(word):
    global track_name
    global current_track
    if word == 'PLAY':
        update_button_text()
        for name in track_names:
            window_2.delete(0, 'end')
            current_track = name

            window_2.configure(state='normal')
            window_2.insert('end', name)

            mixer.init()
            mixer.music.set_volume(100)
            mixer.music.load(f'C:/Users/user/Desktop/python/projects/etc/{name}.mp3')
            mixer.music.play()
            time.sleep(5)

    if word == 'PAUSE':
        mixer.music.pause()
        time.sleep(5)

    if word == 'STOP':
        mixer.music.stop()
        time.sleep(5)

    if word == 'NEXT':
        pass

    if word == 'PREVIOUS':
        pass


def update_button_text():
    button_text.set("PAUSE")


if __name__ == '__main__':
    # create application window
    app = Tk()

    # title
    app.title("Music Player")

    # geometry
    app.geometry('383x121')

    # background color
    app.configure(bg='orange')

    equation = StringVar()
    window_1 = Label(app, textvariable=equation)
    window_1.grid(columnspan=4, ipadx=100, ipady=10)
    equation.set('music player')

    window_2 = Entry(app, width=30)
    window_2.grid(columnspan=4, ipadx=100, ipady=10)
    window_2.configure(state='disabled')

    window_2.grid_columnconfigure((0, 1, 2), uniform="equal", weight=1)

    # Create buttons
    button_text = StringVar()
    button_text.set("PLAY")
    button1 = Button(app, textvariable=button_text, fg='yellow', bg='purple',
                     command=lambda: press(button_text), height=2, width=1)
    button1.grid(row=2, column=0, sticky="NSEW")

    button2 = Button(app, text='STOP', fg='yellow', bg='purple',
                     command=lambda: press('STOP'), height=2, width=1)
    button2.grid(row=2, column=1, sticky="NSEW")

    button3 = Button(app, text='NEXT', fg='yellow', bg='purple',
                     command=lambda: press('NEXT'), height=2, width=1)
    button3.grid(row=2, column=2, sticky="NSEW")

    button4 = Button(app, text='PREVIOUS', fg='yellow', bg='purple',
                     command=lambda: press('PREVIOUS'), height=2, width=1)
    button4.grid(row=2, column=3, sticky="NSEW")

# start the GUI
app.mainloop()

And this is part I'm having trouble with:

    button_text = StringVar()
    button_text.set("PLAY")
    button1 = Button(app, textvariable=button_text, fg='yellow', bg='purple',
                     command=lambda: press(button_text), height=2, width=1)
    button1.grid(row=2, column=0, sticky="NSEW")

I previously used command=lambda: press('PLAY') so that when I click the 'PLAY' button, it goes to def press('PLAY') and so on. But now I'm using StringVar so that I can change the button text after a click, it no longer works. How can I make this work?
Also, when I run the code, and click 'PLAY', the music does play but it is very slow. I tried to show the name of the song first, but seems like always mixer.init() part runs earlier than window_2.insert('end', name) this part. Why is this so?
Thank you!

Upvotes: 1

Views: 1303

Answers (1)

Tresdon
Tresdon

Reputation: 1211

The problem is with:

def press(word):
    if word == 'PLAY':
    ...

the passed in argument is a StringVar so to get the actual string from it you have to use word.get()

Try this:

def press(string_var):
    word = string_var.get()
    if word == 'PLAY':
    ...

it might also be worth to just call this directly instead of from the function, but that's extracurricular:

def update_button_text():
    button_text.set("PAUSE")

Upvotes: 3

Related Questions