如何通过单击python中的按钮开始处理

tmg28

我最近开发了一个 python 程序,它读取 csv 文件并在处理它们之后返回包含图形的 pdf。但是,如果 csv 文件很大,则程序会冻结,直到处理完成。使用这种方法:Link,程序不再冻结,而是自动启动而无需按任何按钮。

这是代码:

try:
    import Tkinter as tk, time, threading, random, Queue as queue
except ModuleNotFoundError:   # Python 3
    import tkinter as tk, time, threading, random, queue

class GuiPart(object):
    def __init__(self, master, queue):
        self.queue = queue
        self.button1 =  tk.Button(master, text="Command", padx=10, 
                            pady=5, fg="white", bg="#263D42", command=ThreadedClient.worker_thread1)

        self.button1.pack()

    def processIncoming(self):
        while self.queue.qsize():
        pass

class ThreadedClient(object):
    """
    Launch the main part of the GUI and the worker thread. periodic_call()
    and end_application() could reside in the GUI part, but putting them
    here means that you have all the thread controls in a single place.
    """
    def __init__(self, master):
        """
        Start the GUI and the asynchronous threads.  We are in the main
        (original) thread of the application, which will later be used by
        the GUI as well.  We spawn a new thread for the worker (I/O).
        """
        self.master = master
        # Create the queue
        self.queue = queue.Queue()

        # Set up the GUI part
        self.gui = GuiPart(master, self.queue)

        # Set up the thread to do asynchronous I/O
        # More threads can also be created and used, if necessary
        self.running = True
        self.thread1 = threading.Thread(target=self.worker_thread1)
        self.thread1.start()

        # Start the periodic call in the GUI to check the queue
        self.periodic_call()


    def periodic_call(self):
        """ Check every 200 ms if there is something new in the queue. """
        self.master.after(200, self.periodic_call)
        self.gui.processIncoming()
        if not self.running:
            # This is the brutal stop of the system.  You may want to do
            # some cleanup before actually shutting it down.
            import sys
            sys.exit(1)


    def worker_thread1(self):

        """
        This is where we handle the asynchronous I/O.  For example, it may be
        a 'select()'.  One important thing to remember is that the thread has
        to yield control pretty regularly, be it by select or otherwise.
        """
    
        while self.running:
            # To simulate asynchronous I/O, create a random number at random
            # intervals. Replace the following two lines with the real thing.
        
            time.sleep(rand.random() * 1.5)

            filenames = filedialog.askopenfilenames(initialdir="/", title="Select File", filetypes = (("comma separated file","*.csv"), ("all files", "*.*"))) #ask user to select the file

    """ based on the data from csv file I am using matplotlib to draw some graphs and then I export them as pdf """

    def end_application(self):
        self.running = False  # Stops worker_thread1 (invoked by "Done" button).

rand = random.Random()
root = tk.Tk()
client = ThreadedClient(root)
root.mainloop()

如果有人可以通过单击按钮帮助我开始处理,我将不胜感激,因为我想添加更多按钮,调用更多功能。

亨利

线程立即启动的原因是因为它在 中__init__,所以在ThreadedClient实例化调用
现在您需要一种启动线程的方法,因此我添加了一个名为start_thread1. 这将创建并启动线程,我们可以从按钮调用此函数。
您需要在ThreadedClient您已经制作的实例中启动线程当前,您创建了一个新实例,但这将不起作用,因为您想使用已有的实例 ( client)。因此,您需要在GuiPart. 我叫过它client_instance
然后您可以使用命令启动线程client_instance.start_thread1程序的其余部分不变。

class GuiPart(object):
    def __init__(self, master, queue, client_instance):
        self.queue = queue
        self.button1 =  tk.Button(master, text="Command", padx=10, 
                            pady=5, fg="white", bg="#263D42", command=client_instance.start_thread1)

        self.button1.pack()

    def processIncoming(self):
        while self.queue.qsize():
            pass

class ThreadedClient(object):
    """
    Launch the main part of the GUI and the worker thread. periodic_call()
    and end_application() could reside in the GUI part, but putting them
    here means that you have all the thread controls in a single place.
    """
    def __init__(self, master):
        """
        Start the GUI and the asynchronous threads.  We are in the main
        (original) thread of the application, which will later be used by
        the GUI as well.  We spawn a new thread for the worker (I/O).
        """
        self.master = master
        # Create the queue
        self.queue = queue.Queue()
        self.running = True

        # Set up the GUI part
        self.gui = GuiPart(master, self.queue, self)
        # Start the periodic call in the GUI to check the queue
        self.periodic_call()

        
    def start_thread1(self):
        # Set up the thread to do asynchronous I/O
        # More threads can also be created and used, if necessary
        thread1 = threading.Thread(target=self.worker_thread1)
        thread1.start()

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何通过片段中的按钮开始活动?

如何通过按下按钮开始处理转换?

PyQt4如何通过单击按钮使用多重处理

Python中的OpenOffice Calc宏-通过单击按钮触发的宏,如何获取该按钮的名称?

如何在HTML中的单击事件上通过下拉按钮从Django中调用外部python脚本

处理CursorAdapter中的按钮单击

如何单击Google趋势中的“加载更多”按钮并通过Selenium和Python打印所有标题

如何通过单击 PyQt4 (python2.7) 中的按钮来更改 QComboBox 值?

如何通过单击javascript中的按钮来运行带有参数的python脚本

如何通过单击导航按钮开始/结束Smoke.js动画?

如何处理jQuery中的按钮单击事件?

如何处理按钮在C#中的单击

如何处理流星中的按钮单击事件?

如何在 wxWidgets 处理程序 OnClose 中单击按钮?

如何在Pupeteer中单击按钮以处理新页面?

如何从另一个按钮中单击一个按钮开始突破for循环

如何通过单击 HTML 网页上的按钮运行 python 脚本?

如何使用Selenium和Python通过链接文本单击按钮

如何使用Selenium和Python通过“ onclick”查找并单击按钮?

如何通过Python使用Selenium单击此按钮

如何通过单击按钮更改JAVA中的字符串?

如何通过单击jQuery中的按钮来更改图像

如何通过单击Angular 7中的按钮来加载组件

如何通过HTML按钮在类中调用PHP函数单击

如何通过单击按钮从浏览器中查看列表

如何通过单击按钮使Kendo网格中的列可编辑

如何通过单击关闭按钮从 prepend() 中删除元素?

如何通过单击按钮获取div html中的文本值

如何通过单击按钮显示本地JSON文件中的数据?