tkinter 等待使用 .invoke() 按下按钮

tkinter wait for button press with .invoke()

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()   

当我选择调用按钮的结果时,文件资源管理器 window 在单击 openButton 之前打开。如何让程序在从按钮调用值之前等待按钮按下?

我试过使用全局变量 True/False 来确定按钮是否被点击。但是,我觉得程序并没有反复检查这个布尔值。也许有一个特定的功能必须添加播放文件?

你的代码

if clicked == True:
            file = str(openButton.invoke())
            playFile(file)      

从未使用过。

您需要做的是在消息框弹出后进行操作

因此

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

变成

def findFile():
    global clicked
    clicked = True
    fileLocation = filedialog.askopenfilename(initialdir = "C:/", title = "Select file", filetypes = (("mp3 files","*.mp3"), ("m4a files", ".m4a"), ("all files","*.*")))
    if fileLocation:
          playFile(fileLocation)
    return fileLocation

完整的工作示例

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","*.*")))
    playFile(fileLocation)
    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")
    app = Example()
    root.mainloop()

if __name__ == '__main__':
    main()