Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 带螺纹钢筋混凝土的进度_Python_Multithreading_Tkinter - Fatal编程技术网

Python 带螺纹钢筋混凝土的进度

Python 带螺纹钢筋混凝土的进度,python,multithreading,tkinter,Python,Multithreading,Tkinter,大家好,我有一个问题,tkinter中有一个进度条,您将看到: 我执行一个方法,我希望在执行这个操作时,出现一个进度条,问题是当方法结束时,进度条不会停止,我不知道怎么做。。。请帮帮忙 import threading import tkinter as tk import tkinter.ttk as ttk class App(tk.Tk): def __init__(self): super().__init__() self.title(&qu

大家好,我有一个问题,tkinter中有一个进度条,您将看到:

我执行一个方法,我希望在执行这个操作时,出现一个进度条,问题是当方法结束时,进度条不会停止,我不知道怎么做。。。请帮帮忙

import threading
import tkinter as tk
import tkinter.ttk as ttk


class App(tk.Tk):
    def __init__(self):
        super().__init__()
        self.title("Progressbar example")
        self.button = tk.Button(self, text="Start", command=self.start_action)
        self.button.pack(padx=10, pady=10)

    def start_action(self):
        self.button.config(state=tk.DISABLED)
        t = threading.Thread(target=self.contar)
        windows_bar = WinProgressBar(self)
        self.after(t.start())

    def contar(self):
        for i in range(1000000):
            print("está contando", i)
        return True


class WinProgressBar(tk.Toplevel):
    def __init__(self, parent):
        super().__init__(parent)
        self.title("Barr progress")
        self.geometry("300x200")
        self.progressbar = ttk.Progressbar(self, mode="indeterminate")
        self.progressbar.place(x=30, y=60, width=200)
        self.progressbar.start(20)


if __name__ == "__main__":
    app = App()
    app.mainloop()

你可以试着用威胁来阻止它

def contar(self):
    for i in range(500):
        print("está contando", i)
    
    self.windows_bar.progressbar.stop()
    self.windows_bar.destroy()
    self.button.config(state=tk.NORMAL)


许多GUI框架不喜欢更改线程中的小部件,然后您可以使用变量
self.running
(这将在两个线程之间共享)来通知程序线程是否正在运行

def contar(self):
    self.running = True

    for i in range(500):
        print("está contando", i)

    self.running = False
在主线程中,您可以使用
after()
定期检查此变量以停止progressbar和/或关闭窗口,而这一次主线程正在更改小部件

def check_thread(self):
    if self.running:
        self.after(1000, self.check_thread) # run again after 1s (1000ms)
    else:
        print('stop')
        self.windows_bar.progressbar.stop()
        self.windows_bar.destroy()
        self.button.config(state=tk.NORMAL)


如果线程不能共享变量,那么您可以使用
队列
将信息从线程发送到主线程

import threading
import queue
import tkinter as tk
import tkinter.ttk as ttk


class App(tk.Tk):
    def __init__(self):
        super().__init__()
        self.title("Progressbar example")
        self.button = tk.Button(self, text="Start", command=self.start_action)
        self.button.pack(padx=10, pady=10)

    def start_action(self):
        self.button.config(state=tk.DISABLED)
        
        self.q = queue.Queue()
        t = threading.Thread(target=self.contar, args=(self.q,))
        t.start()

        self.windows_bar = WinProgressBar(self)
        
        self.check_queue()
        
    def contar(self, q):
        for i in range(200):
            print("está contando", i)
        
        q.put('finished')
        
    def check_queue(self):
        if self.q.empty() or self.q.get() != 'finished':
            self.after(1000, self.check_queue)
        else:
            print('stop')
            self.windows_bar.progressbar.stop()
            self.windows_bar.destroy()
            self.button.config(state=tk.NORMAL)
        
        
class WinProgressBar(tk.Toplevel):
    def __init__(self, parent):
        super().__init__(parent)
        self.title("Barr progress")
        self.geometry("300x200")
        self.progressbar = ttk.Progressbar(self, mode="indeterminate")
        self.progressbar.place(x=30, y=60, width=200)
        self.progressbar.start(20)


if __name__ == "__main__":
    app = App()
    app.mainloop()

after()
需要函数名而不带
()
-
self.after(t.start)
-但所有这行似乎都没有用。
线程开始时可以设置
finished=False
,结束时设置
finished=True
。主代码应该使用
self.after()
运行代码,该代码将检查
finished==True
并停止ProrgesBar。但是更安全的方法是使用
query
而不是变量
finished
<代码>线程应将信息放入队列,主代码应使用
after()
检查队列中是否有信息并停止进度条。
import threading
import tkinter as tk
import tkinter.ttk as ttk


class App(tk.Tk):
    def __init__(self):
        super().__init__()
        self.title("Progressbar example")
        self.button = tk.Button(self, text="Start", command=self.start_action)
        self.button.pack(padx=10, pady=10)

    def start_action(self):
        self.button.config(state=tk.DISABLED)
        
        t = threading.Thread(target=self.contar)
        t.start()

        self.windows_bar = WinProgressBar(self)

        self.check_thread() # run first time
        
    def contar(self):
        self.running = True
        for i in range(500):
            print("está contando", i)
        self.running = False

    def check_thread(self):
        if self.running:
            self.after(1000, self.check_thread) # run again after 1s (1000ms)
        else:
            print('stop')
            self.windows_bar.progressbar.stop()
            self.windows_bar.destroy()
            self.button.config(state=tk.NORMAL)
        
        
class WinProgressBar(tk.Toplevel):
    def __init__(self, parent):
        super().__init__(parent)
        self.title("Barr progress")
        self.geometry("300x200")
        self.progressbar = ttk.Progressbar(self, mode="indeterminate")
        self.progressbar.place(x=30, y=60, width=200)
        self.progressbar.start(20)


if __name__ == "__main__":
    app = App()
    app.mainloop()
import threading
import queue
import tkinter as tk
import tkinter.ttk as ttk


class App(tk.Tk):
    def __init__(self):
        super().__init__()
        self.title("Progressbar example")
        self.button = tk.Button(self, text="Start", command=self.start_action)
        self.button.pack(padx=10, pady=10)

    def start_action(self):
        self.button.config(state=tk.DISABLED)
        
        self.q = queue.Queue()
        t = threading.Thread(target=self.contar, args=(self.q,))
        t.start()

        self.windows_bar = WinProgressBar(self)
        
        self.check_queue()
        
    def contar(self, q):
        for i in range(200):
            print("está contando", i)
        
        q.put('finished')
        
    def check_queue(self):
        if self.q.empty() or self.q.get() != 'finished':
            self.after(1000, self.check_queue)
        else:
            print('stop')
            self.windows_bar.progressbar.stop()
            self.windows_bar.destroy()
            self.button.config(state=tk.NORMAL)
        
        
class WinProgressBar(tk.Toplevel):
    def __init__(self, parent):
        super().__init__(parent)
        self.title("Barr progress")
        self.geometry("300x200")
        self.progressbar = ttk.Progressbar(self, mode="indeterminate")
        self.progressbar.place(x=30, y=60, width=200)
        self.progressbar.start(20)


if __name__ == "__main__":
    app = App()
    app.mainloop()