简体   繁体   English

Tkinter 按钮没有出现,对不起,我知道一个线程已经存在

[英]Tkinter button does not appear, sorry guys I know that a thread already exists

The button it does not appear, any tips guys?.它没有出现的按钮,有什么提示吗? The screenshot func works fine but not the button, I have to close it on my own.屏幕截图功能可以正常工作,但按钮不行,我必须自己关闭它。

import time
from tkinter import *
import pyautogui

class App:

    def __init__(self, master, task):

        frame = Frame(master)
        frame.pack()

        self.button = Button(
            frame, text="QUIT", fg="red", command=frame.quit
            )
        self.button.pack(side=LEFT)
        frame.after(0,task)

def task():
    i = 1
    while i > 0:
        myScreenshot = pyautogui.screenshot()
        myScreenshot.save(r"C:\Users\elsin\Desktop\python\nameTask" + str(i) + ".png")
        time.sleep(2)
        i += 1

root = Tk()

app = App(root,task())
root.mainloop()

Error:错误:

C:\Users\elsin\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/elsin/PycharmProjects/pythonProject/1.py
Traceback (most recent call last):
  File "C:\Users\elsin\PycharmProjects\pythonProject\1.py", line 28, in <module>
    app = App(root,task())
  File "C:\Users\elsin\PycharmProjects\pythonProject\1.py", line 23, in task
    time.sleep(2)
KeyboardInterrupt

Process finished with exit code -1073741510 (0xC000013A: interrupted by Ctrl+C)

First: you have to send function's name without () .首先:您必须发送不带()的函数名称。

At this moment you have something like此时你有类似的东西

result = task()

app = App(root, result)

but task() runs while -loop which runs all time and code never goes to line App(root, result) - so it can't display it.但是task()运行while -loop 一直运行并且代码永远不会进入行App(root, result) - 所以它无法显示它。

If you send function's name then you will have another problem - your frame.after(0, task) starts this task at once and it blocks rest of code.如果您发送函数的名称,那么您将遇到另一个问题 - 您的frame.after(0, task)立即启动此任务并阻止 rest 代码。

If you use some delay frame.after(100, task) then it will have time to display window with button.如果您使用一些延迟frame.after(100, task)那么它将有时间用按钮显示 window 。

But it still have problem with task which runs while -loop and it blocks但是它仍然存在运行while -loop 并且阻塞的task的问题 mainloop and tkinter` is freezed. mainloop and tkinter` 被冻结。


The main problem is that you have two loops which have to work at the same time:主要问题是您有两个必须同时工作的循环:

  • first: mainloop() ,第一个: mainloop()
  • second: while -loop第二: while循环

and this need to run one of them in separated thread, or you have to use root.after(2000, task) instead of while + sleep()这需要在单独的线程中运行其中一个,或者您必须使用root.after(2000, task)而不是while + sleep()

BTW: you should use master.destroy instead of frame.quit to stop tkinter program顺便说一句:您应该使用master.destroy而不是frame.quit来停止 tkinter 程序

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

class App:

    def __init__(self, master, task):

        frame = tk.Frame(master)
        frame.pack()

        self.button = tk.Button(frame, text="QUIT", fg="red", command=master.destroy)
        self.button.pack(side='left')
        
        frame.after(100, task, 1)

def task(i):
    myScreenshot = pyautogui.screenshot()
    myScreenshot.save(r"C:\Users\elsin\Desktop\python\nameTask" + str(i) + ".png")
    i += 1
    root.after(2000, task, i)

root = tk.Tk()
app = App(root, task)
root.mainloop()

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM