3 回答
TA贡献1810条经验 获得超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贡献1900条经验 获得超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()
这里同一个按钮用于播放、暂停和恢复。我使用了lambda具有语法的函数lambda arguments: expression。task每次单击按钮时,值都会更改。
TA贡献1833条经验 获得超4个赞
我使用您的代码创建了这个示例。
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()
添加回答
举报