【问题标题】:Making TKinter Loops制作 TKinter 循环
【发布时间】:2013-12-15 02:31:36
【问题描述】:

我遇到了一些问题,因为我需要暂停某些功能,但我决定使用变量 frame 创建一个系统,该变量每毫秒递增一次,但我再次暂停时遇到了麻烦。

这是我当前的代码:

from Tkinter import *
import ImageTk, threading, time

frame = 0

def waitFrames(milliseconds, begin):
    global frame
    if frame < begin+milliseconds:
        waitFrames(3000, begin)

def scrollArrow():
    global frame
    waitFrames(3000, frame)
    print("test")

def update():
    global frame
    frame += 1
    print(frame)
    root.after(100, update)

####-------####
####DIVIDER####
####-------####

root = Tk()
canvas = Canvas(root,height=480,width=600)
canvas.pack()

threading.Thread(target=update()).start()
scrollArrow()

root.mainloop()

我的主要问题是它给了我错误:

Traceback (most recent call last):
  File "C:\SongMaster\v2\times.py", line 31, in <module>
    scrollArrow()
  File "C:\SongMaster\v2\times.py", line 13, in scrollArrow
    waitFrames(3000, frame)
  File "C:\SongMaster\v2\times.py", line 9, in waitFrames
    waitFrames(3000, begin)

关于如何解决这个问题的任何提示?

按照 furas 的要求,这是我的 OTHER 程序中的代码,我正在尝试重写:

from Tkinter import *
from random import randint
import ImageTk
import winsound, sys, threading, time

combo = 0

def playSong():
    global start
    start = time.clock()

    def func():
        winsound.PlaySound('song2.wav', winsound.SND_ALIAS)
        print("hi")

    threading.Thread(target=func).start()

def deleteImage(imageObject):
    canvas.delete(imageObject)

def scrollToTop(imaget):
    global combo
    if imaget in canvas.find_all() and canvas.coords(imaget)[1] > 0:
        if canvas.coords(imaget)[1] == 80.0:
            print("perfect")
        canvas.move(imaget, 0, -1)
        t.after(2, lambda:scrollToTop(imaget))
    else:
        deleteImage(imaget)

def getClosest(x):
    global combo
    closest = canvas.find_withtag("token")
    distances = [len(closest)]
    for i in range(0,len(closest)):
        coords = canvas.coords(closest[i])
        if coords[0] == x and coords[1] < 120:
            deleteImage(closest[i])
            if coords[1] > 75 and coords[1] < 90:
                combo += 1
                perfect = canvas.create_image(300,300,image=perfectImg)
                t.after(500, lambda:deleteImage(perfect))
            elif coords[1] < 20:
                combo = 0
            else:
                combo = 0
            v.set(combo)
            break


def left(event):
    i = canvas.create_image(210,80,image=bigArrowImg[0])
    t.after(100, lambda:deleteImage(i))
    getClosest(210)

def down(event):
    i = canvas.create_image(270,80,image=bigArrowImg[1])
    t.after(100, lambda:deleteImage(i))
    getClosest(270)

def up(event):
    i = canvas.create_image(330,80,image=bigArrowImg[2])
    t.after(100, lambda:deleteImage(i))
    getClosest(330)

def right(event):
    i = canvas.create_image(390,80,image=bigArrowImg[3])
    t.after(100, lambda:deleteImage(i))
    getClosest(390)

def makeNote(direction):
    t.update()
    global times, start
    start = time.clock()

    times += 1
    arrow = canvas.create_image(pos[direction],480,image=arrowImg[direction],tags="token")
    threading.Thread(target=scrollToTop(arrow)).start()
    #scrollToTop(arrow)

    if(times < 16):
        newDirection = randint(0,3)
        t.after(500, lambda:makeNote(newDirection))



t = Tk()
canvas = Canvas(t,height=480,width=600)
canvas.pack()

bigArrowImg = [ImageTk.PhotoImage(file="images/bigLeft.png"),
               ImageTk.PhotoImage(file="images/bigDown.png"),
               ImageTk.PhotoImage(file="images/bigUp.png"),
               ImageTk.PhotoImage(file="images/bigRight.png")]
backgroundImg = ImageTk.PhotoImage(file="images/background.png")
arrowImg = [ImageTk.PhotoImage(file="images/leftArrow.png"),
            ImageTk.PhotoImage(file="images/downArrow.png"),
            ImageTk.PhotoImage(file="images/upArrow.png"),
            ImageTk.PhotoImage(file="images/rightArrow.png")]
perfectImg = ImageTk.PhotoImage(file="images/PERFECT.png")
pos = [210, 270, 330, 390]

background = canvas.create_image(300,240,image=backgroundImg)
#arrow = canvas.create_image(210,300,image=arrowImg[0],tags="token")
#scrollToTop(arrow)

times = 0

v = StringVar()
Label(t, textvariable=v).pack()
v.set("0")

makeNote(0)
playSong()

t.bind('a', left)
t.bind('d', down)
t.bind('l', up)
t.bind('\'', right)

while 1:
    t.update()

这段代码的问题是我不能在不延迟箭头的情况下延迟 playSong...

【问题讨论】:

    标签: python canvas tkinter traceback


    【解决方案1】:

    waitFrames 中,你调用waitFrames,然后调用waitFrames,再调用waitFrames 等等。它被称为“递归”——Python 的递归限制为 100 个。计算机可以在一秒钟内执行 10 000 次(甚至更多)递归。不要在waitFrames 中使用递归。

    使用root.after() 延迟操作。

    def scrollArrow():
         root.after(3000, printTest)
    
    def printTest():
         print("test")
    

    编辑:playSound()回答新代码

    我没有 Windows,所以我不能使用 winsound,所以我只用 print("hi") 测试了代码

    makeNote(0)
    
    t.after(3000, playSong) # run playSong after 3 seconds
    
    t.bind('a', left)
    

    【讨论】:

    • 嗯。再次感谢 furas,但不完全是我想要的。你看,我在使用 root.after() 时遇到了问题,因为它有时看起来有点不准确,但我也试图将声音延迟播放一秒钟。不工作:'(
    • 我认为如果您使用waitFramestime.sleep(),您不仅会延迟声音,还会延迟程序中的所有功能。
    • 我尝试 t.after(3000, playSong) 在 3 秒后运行 playSound - 它有效。但我无法使用winsound 对其进行测试——我不使用 Windows。但是我看到“hi”有 3 秒的延迟,我可以在看到“hi”之前使用键盘。
    • 抱歉帮不了你了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-27
    相关资源
    最近更新 更多