GUI无法正常工作

时间:2015-12-19 08:37:42

标签: python image user-interface audio tkinter

我想制作一个30秒的倒计时时钟,就像在电视节目中一样,有声音。

但是当我运行此代码时,声音会播放,而GUI则不会响应,因此不会出现时钟。

当我将这两个部分中的一个放入(时钟或声音)时,它们可以正常工作,但是当我将它们应用到一个程序中时它就不起作用了。

import tkinter as tk
from winsound import *
import time
def count_down():
    # start with 2 minutes --> 120 seconds
    for t in range(30, -1, -1):
        # format as 2 digit integers, fills with zero to the left
        # divmod() gives minutes, seconds
        sf = "{:02d}:{:02d}".format(*divmod(t, 60))
        #print(sf)  # test
        time_str.set(sf)
        root.update()
        # delay one second
        time.sleep(1)
# create root/main window
root = tk.Tk()
time_str = tk.StringVar()
# create the time display label, give it a large font
# label auto-adjusts to the font
label_font = ('calibri', 40)
tk.Label(root, textvariable=time_str, font=label_font, bg='black', fg='blue', relief='raised', bd=3).pack(fill='x', padx=5, pady=5)
# create start and stop buttons
# pack() positions the buttons below the label
play = lambda: PlaySound('countdown_clock.wav', SND_FILENAME)
photo2=tk.PhotoImage(file="play button2.png")
tk.Button(root, image=photo2, bg='black', padx=2, pady=2, command=count_down and play).pack()
# stop simply exits root window
photo=tk.PhotoImage(file="stop button.png")
tk.Button(root, image=photo, bg='black', padx=2, pady=2, command=root.destroy).pack()

# start the GUI event loop
root.mainloop()

1 个答案:

答案 0 :(得分:1)

JButton[]将返回count_down and play;因此,单击按钮时不会调用play

count_down

你需要调整它来调用这两个函数:

>>> a = 'a_truth_value'
>>> b = 'another_truth_value'
>>> a and b
'another_truth_value'