简体   繁体   English

使用 Python 和 Tkinter,我将如何运行 .mainloop() 的每个循环的代码?

[英]Using Python and Tkinter, How would I run code every loop of .mainloop()?

I have a program where I need to move an image object every time the mainloop() loops.我有一个程序,每次 mainloop() 循环时,我都需要移动图像 object。 I haven't tried doing much, mostly because I don't know where to start.我没有尝试做太多,主要是因为我不知道从哪里开始。 I made a dummy version of my project that simulates the issue I'm having.我制作了一个模拟我遇到的问题的项目的虚拟版本。

from tkinter import *

window = tk.Tk()
window.geometry('%ix%i+400+0' % (500, 600))

canvas = Canvas(window, width=500, height=600, bg='white')
canvas.pack()

w, x, y, z = 300, 300, 200, 200

x = canvas.create_rectangle(w, x, y, z)

def moveRectangle():
   canvas.move(x, 10, 0)

# Run the moveRectangle function everytime the mainloop loops
window.mainloop()

To sum up my issue, I need to run mainloop as if it isn't a blocking function.总结一下我的问题,我需要运行 mainloop,就好像它不是阻塞 function 一样。 Rather, either run it asynchronous, or maybe pause it and then run the function, though I don't think that's possible.相反,要么异步运行它,要么暂停它,然后运行 function,尽管我认为这是不可能的。

Anything helps Thanks有什么帮助谢谢

Mainloop in tkinter doesn't loop through your code. tkinter 中的主循环不会遍历您的代码。 It's looping through list of events.它循环遍历事件列表。 You can create an event by clicking buttons etc. Another way is that you can call commands like update() or update_idletasks() .您可以通过单击按钮等来创建事件。另一种方法是您可以调用诸如update()update_idletasks()类的命令。 Combinig that with after() can give you results you are looking for.将其与after()结合可以为您提供所需的结果。 So look up these in documentation, it will be helpful.因此,请在文档中查找这些内容,这将很有帮助。 Also you can read: understanding mainloop .您还可以阅读:了解 mainloop

def moveRectangle():
    canvas.move(x, 10, 0)
    for i in range(20):  # 20 moves 10px every 50ms
        window.after(50, canvas.move(x, 10, 0))
        window.update()
moveRectangle()

This little code above demonstrate how you could use mentioned commands to get your object move on screen.上面这段小代码演示了如何使用上述命令让 object 在屏幕上移动。

I think there are ways to set up a custom mainloop() which could update your UI every time the loop runs but depending on what you want to do the more usual method is to use after.我认为有一些方法可以设置一个自定义的mainloop() ,它可以在每次循环运行时更新你的 UI,但取决于你想要做什么,更常用的方法是在之后使用。 By arranging for the function called by after to call itself with after an effective loop can be created.通过安排由 after 调用的 function 调用自身,可以创建一个有效的循环。

I've amended you code to bounce the rectangle as it reaches the sides of the canvas so it can show something as it runs indefinitely.我已经修改了您的代码,以便在矩形到达 canvas 的侧面时反弹矩形,这样它就可以在无限期运行时显示一些东西。

import tkinter as tk

WAIT = 10 # in milliseconds, can be zero

window = tk.Tk()
window.geometry('%ix%i+400+0' % (500, 600))

canvas = tk.Canvas(window, width=500, height=600, bg='white')
canvas.pack()

w, x, y, z = 300, 300, 200, 200

x = canvas.create_rectangle(w, x, y, z)

amount = 10

def direction( current ):
    x0, y0, x1, y1 = canvas.bbox( x )
    if x0 <= 0:
        return 10   # Move rect right
    elif x1 >= 500:
        return -10  # Move rect left
    else:
        return current

def moveRectangle():
    global amount
    canvas.move(x, amount, 0)
    window.update()

    # Change Direction as the rectangle hits the edge of the canvas.
    amount = direction( amount )

    window.after( WAIT, moveRectangle )
    #              ms ,   function
    # Loop implemented by moveRectangle calling itself.

window.after( 1000, moveRectangle )
# Wait 1 second before starting to move the rectangle.

window.mainloop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何测试带有 Tkinter 主循环的 Python 代码? - How can Python code with Tkinter mainloop be tested? Python,Tkinter:如何使用线程防止tkinter gui mainloop崩溃 - Python, Tkinter: how can I prevent tkinter gui mainloop crash using threading 导入 Tkinter 文件后如何继续运行代码? - How would I continue to run code after importing a Tkinter file? 如何在不中断tkinter主循环的情况下运行函数,又如何从该函数向主循环中的小部件发送信息? - How can I run a function without interrupting my tkinter mainloop, but also send information from that function to widgets in my mainloop? 如何在Python的tkinter mainloop()中刷新字典? - How to refresh a dictionary in a tkinter mainloop() in Python? Python如何为Unittests压缩tkinter mainloop() - Python how to supress tkinter mainloop() for Unittests Tkinter mainloop()未运行-Python - Tkinter mainloop() not running - Python 如何在运行 tkinter 主循环的同时等待循环返回值? - How do I wait for a loop to return a value while also running a tkinter mainloop? Python:有什么方法可以在后台运行mainloop()吗? - Python: Is there a way I can run mainloop() in the background? 我将如何制作每次在 tkinter 中显示帧时运行的方法 - How would I make a method which is run every time a frame is shown in tkinter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM