3 回答

TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超5個贊
使用帶有 if 條件
示例的檢查按鈕文本:
from tkinter import Tk, Button
from pygame import mixer
root = Tk()
root.title("Play Music")
root.geometry('350x200')
mixer.init()
mixer.music.load("sound.mp3")
def play_music():
if button["text"] == "Play":
button["text"] = "Pause"
button["bg"] = "red"
mixer.music.play()
else:
button["text"] = "Play"
button["bg"] = "green"
mixer.music.pause()
button = Button(root, text='Play', width=14, bg='green', fg='black', command=play_music)
button.pack()
root.mainloop()

TA貢獻(xiàn)1900條經(jīng)驗(yàn) 獲得超5個贊
這是一個例子。
from tkinter import *
root = Tk()
def doSomething(task):
global var, button
if task == 0:
var.set("Started Playing")
button.configure(text = "Pause")
button.configure(command = lambda task = 1: doSomething(task))
#play
elif task == 1:
var.set("Paused")
button.configure(text = "Resume")
button.configure(command = lambda task = 0: doSomething(task))
#pause
button = Button(root, text = "Play", command = lambda task = 0: doSomething(task))
button.pack()
var = StringVar()
label = Label(root, textvariable = var)
label.pack()
root.mainloop()
這里同一個按鈕用于播放、暫停和恢復(fù)。我使用了lambda具有語法的函數(shù)lambda arguments: expression。task每次單擊按鈕時,值都會更改。

TA貢獻(xiàn)1833條經(jīng)驗(yàn) 獲得超4個贊
我使用您的代碼創(chuàng)建了這個示例。
from tkinter import *
import pygame
root = Tk()
root.title("MSC")
pygame.init()
mxstate = 0 # music play state
pygame.mixer.music.load('tavernsound.mp3')
def Play_music():
? ? ?global mxstate
? ? ?if mxstate == 0:? # music not started
? ? ? ? ?pygame.mixer.music.play()
? ? ? ? ?btn11.configure(text = "Pause")
? ? ? ? ?mxstate =? 1
? ? ? ? ?return
? ? ? ??
? ? ?if mxstate == 1:? # music playing
? ? ? ? ?pygame.mixer.music.pause()
? ? ? ? ?btn11.configure(text = "Resume")
? ? ?else:? # music paused
? ? ? ? ?pygame.mixer.music.unpause()
? ? ? ? ?btn11.configure(text = "Pause")
? ? ?mxstate = 3-mxstate? # swap pause state
? ? ?
btn11=Button(root, text='Ambiance', width=14, bg='red', fg='black', command=Play_music)
btn11.pack(side=LEFT)
root.mainloop()
添加回答
舉報(bào)