使用 Tkinter、Threading 和 After 方法 [英] Using Tkinter, Threading and After method

查看:37
本文介绍了使用 Tkinter、Threading 和 After 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 python 中的 tkinter 进行 selenium 项目.我想一次执行多个 selenium 任务(最多可以达到 100 个),所以我使用线程来完成这个目标,但是我遇到了一个问题,因为我需要所有单独的 selenium 任务等待几秒钟在指令之间,我使用了 '.after' 方法,但由于某种原因,我的程序仍然冻结,我做了很多研究,但似乎还没有找到答案.有没有办法在 python 中使用 tkinter、线程和某种睡眠?我需要切换到多处理吗?但我需要所有 selenium 进程在相同的 2 分钟内完成,每个进程大约需要一分钟.(例如,我在 6:00 开始 100 个,1 分钟的进程,所有任务都需要在 6:02 之前完成)

im working on a selenium project using tkinter in python. I want to execute many selenium task at one time(could be up to 100), so I used threading to accomplish this goal, but I came to a problem, due to the fact that I need all individual selenium task to wait a couple seconds between instructions, I used the '.after' method but for some reason my program is still freezing up, I've done a lot of research but I cant seem to find the answer yet. Isn't there a way to use tkinter, threading and some sort of sleeping in python? Do I need to switch to multiprocessing? but I would need all selenium processes to finish within the same 2 minutes and each process takes about a minute.(e.g lets say i start 100, 1 minute processes at 6:00 all task would need to be finished by 6:02)

我创建了模仿我的 selenium 脚本的最少代码,因此更易于阅读,这里是:

I created minimal code which mimics my selenium script, so its easier to read, here it is:

from tkinter import *
import tkinter as tk
import time
root = tk.Tk()
root.geometry('700x700')
import threading
class Make:
    def __init__(self,num):
        self.num = num.get()
        Label(root,text='HELLO WORLD WILL PRINT: '+str(self.num)+' times, press go to confirm').pack()
        Button(root, text='go', command=lambda: self.starting()).pack()

    def starting(self):

        for count in range(self.num):
            t = threading.Thread(target=gogogo())
            t.start()

def gogogo():
    tk.Label(root,text='HELLO WORLD').pack()
    root.after(2000,print('Please wait 2 seconds'))
    tk.Label(root,text='You are inside my world').pack()


Label(root,text='How many times should I print: HELLO WORLD').pack()

num = IntVar()
Entry(root, textvariable=num).pack()

Button(root, text='Start', command=lambda: Make(num)).pack()

root.mainloop()

推荐答案

你的主要问题是 after()Thread() 类似于 command= 只需要函数名,不需要(),后面会使用() 来执行.

You main problem is that after() and Thread() similar to command= needs only function's name without () and it will later use () to execute it.

另一个问题是after()不会停止代码,它只会在2000ms之后向mainloop发送信息来执行函数Python 在 after() 之后的下一行立即运行.您必须在 after()

Other problem is that after() doesn't stop code but it only send information to mainloop to execute function after 2000ms and later Python runs at once next line after after(). You have to add Label in fucntion executed by after()

def gogogo():
    tk.Label(root, text='HELLO WORLD').pack()
    print('Please wait 2 seconds')
    root.after(2000, next_message) # function's name without ()

def next_message():
    tk.Label(root, text='You are inside my world').pack()

<小时>

# from tkinter import * # PEP8: `import *` is not preferred
import tkinter as tk
import threading
import time

class Make:

    def __init__(self, num):
        self.num = num.get()

        text = 'HELLO WORLD WILL PRINT: {} times, press go to confirm'.format(self.num)
        tk.Label(root, text=text).pack()

        tk.Button(root, text='go', command=self.starting).pack()

    def starting(self):
        for count in range(self.num):
            t = threading.Thread(target=gogogo) # function's name without ()
            t.start()

def gogogo():
    tk.Label(root, text='HELLO WORLD').pack()
    print('Please wait 2 seconds')
    root.after(2000, next_message) # function's name without ()

def next_message():
    tk.Label(root, text='You are inside my world').pack()

# --- main ---

root = tk.Tk()
root.geometry('700x700')

tk.Label(root, text='How many times should I print: HELLO WORLD').pack()

num = tk.IntVar()
tk.Entry(root, textvariable=num).pack()

tk.Button(root, text='Start', command=lambda:Make(num)).pack()

root.mainloop()

<小时>

因为 gogogo() 在分离的线程中运行,所以你也可以使用 time.sleep() 因为它不会阻塞mainloop() 在主线程中


Because gogogo() runs in separated thread so you can also use time.sleep() because it doesn't block mainloop() in main thread

def gogogo():
    tk.Label(root, text='HELLO WORLD').pack()
    print('Please wait 2 seconds')
    time.sleep(2)
    tk.Label(root, text='You are inside my world').pack()

这篇关于使用 Tkinter、Threading 和 After 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆