【发布时间】:2018-10-13 22:21:06
【问题描述】:
import time
from pygame import mixer
from tkinter import filedialog, Tk, BOTH
from tkinter.ttk import Frame, Button
from tkinter import *
def playFile(filePath, interval = 5, playTime = 60):
playCount = int(playTime//interval)
for play in range(0, playCount):
mixer.init()
mixer.music.load(filePath)
mixer.music.play()
time.sleep(interval*60)
global clicked
clicked = False
def findFile():
global clicked
clicked = True
fileLocation = filedialog.askopenfilename(initialdir = "C:/", title = "Select file", filetypes = (("mp3 files","*.mp3"), ("m4a files", ".m4a"), ("all files","*.*")))
return fileLocation
file = ''
class Example(Frame):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.master.title("Interval Player")
self.pack(fill=BOTH, expand = 1)
openButton = Button(self, text = "Open", command=findFile)
openButton.place(x=0, y=0)
def main():
root = Tk()
root.geometry("250x150+300+300")
if clicked == True:
file = str(openButton.invoke())
playFile(file)
app = Example()
root.mainloop()
if __name__ == '__main__':
main()
当我选择调用按钮的结果时,文件资源管理器窗口会在单击 openButton 之前打开。如何让程序在从按钮调用值之前等待按钮按下?
我尝试使用带有 True/False 的全局变量来查找按钮是否已被单击。但是,我觉得程序没有重复检查这个布尔值。也许有一个特定的功能必须添加 playFile?
【问题讨论】:
-
您似乎使这比它需要的复杂得多。只需让您的
findFile()直接致电playFile();不需要全局变量,或者你认为.invoke()为你做的任何事情。
标签: python button tkinter invoke