1. <legend id='Cnj3s'><style id='Cnj3s'><dir id='Cnj3s'><q id='Cnj3s'></q></dir></style></legend>

        • <bdo id='Cnj3s'></bdo><ul id='Cnj3s'></ul>

        <small id='Cnj3s'></small><noframes id='Cnj3s'>

        <i id='Cnj3s'><tr id='Cnj3s'><dt id='Cnj3s'><q id='Cnj3s'><span id='Cnj3s'><b id='Cnj3s'><form id='Cnj3s'><ins id='Cnj3s'></ins><ul id='Cnj3s'></ul><sub id='Cnj3s'></sub></form><legend id='Cnj3s'></legend><bdo id='Cnj3s'><pre id='Cnj3s'><center id='Cnj3s'></center></pre></bdo></b><th id='Cnj3s'></th></span></q></dt></tr></i><div id='Cnj3s'><tfoot id='Cnj3s'></tfoot><dl id='Cnj3s'><fieldset id='Cnj3s'></fieldset></dl></div>
        <tfoot id='Cnj3s'></tfoot>

        Python tkinter 中的多处理

        时间:2023-05-25
      1. <legend id='xzZ5J'><style id='xzZ5J'><dir id='xzZ5J'><q id='xzZ5J'></q></dir></style></legend><tfoot id='xzZ5J'></tfoot>
          <tbody id='xzZ5J'></tbody>

      2. <small id='xzZ5J'></small><noframes id='xzZ5J'>

          <bdo id='xzZ5J'></bdo><ul id='xzZ5J'></ul>

          • <i id='xzZ5J'><tr id='xzZ5J'><dt id='xzZ5J'><q id='xzZ5J'><span id='xzZ5J'><b id='xzZ5J'><form id='xzZ5J'><ins id='xzZ5J'></ins><ul id='xzZ5J'></ul><sub id='xzZ5J'></sub></form><legend id='xzZ5J'></legend><bdo id='xzZ5J'><pre id='xzZ5J'><center id='xzZ5J'></center></pre></bdo></b><th id='xzZ5J'></th></span></q></dt></tr></i><div id='xzZ5J'><tfoot id='xzZ5J'></tfoot><dl id='xzZ5J'><fieldset id='xzZ5J'></fieldset></dl></div>

                  本文介绍了Python tkinter 中的多处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  如何在没有多线程的情况下在 python 中运行多个进程?例如考虑以下问题:-

                  How to run multiple processes in python without multithreading? For example consider the following problem:-

                  我们必须制作一个 Gui,它有一个开始按钮,用于启动一个函数(例如,打印所有整数),还有一个停止按钮,这样点击它就会停止函数.

                  We have to make a Gui,which has a start button which starts a function(say, prints all integers) and there is a stop button, such that clicking it stops the function.

                  如何在 Tkinter 中做到这一点?

                  How to do this in Tkinter?

                  推荐答案

                  然后您需要将 Button 小部件与启动工作线程的函数绑定.例如:

                  Then you need to bind the Button widget with the function which starts the working thread. For example:

                  import time
                  import threading
                  import Tkinter as tk
                  
                  class App():
                      def __init__(self, root):
                          self.button = tk.Button(root)
                          self.button.pack()
                          self._resetbutton()
                      def _resetbutton(self):
                          self.running = False
                          self.button.config(text="Start", command=self.startthread)
                      def startthread(self):
                          self.running = True
                          newthread = threading.Thread(target=self.printints)
                          newthread.start()
                          self.button.config(text="Stop", command=self._resetbutton)
                      def printints(self):
                          x = 0
                          while self.running:
                              print(x)
                              x += 1
                              time.sleep(1) # Simulate harder task
                  

                  使用 self.running 方法,您只能通过更改线程的值来优雅地结束线程.请注意,使用多个线程可以避免在执行 printints 时阻塞 GUI.

                  With the self.running approach, you can end the thread gracefully only by changing its value. Note that the use of multiple threads serves to avoid blocking the GUI while printints is being executed.

                  我已阅读 this previous question,我想您为什么在这里明确要求解决方案没有多线程.在 Tkinter 中,此解决方案可用于其他线程必须与 GUI 部分通信的场景.例如:在渲染某些图像时填充进度条.

                  I have read this previous question and I suppose why you explicitly asked here for a solution without multithreading. In Tkinter this solution can be used in a scenario where the other threads have to communicate with the GUI part. For example: filling a progressbar while some images are being rendered.

                  但是,正如评论中所指出的那样,这种方法对于仅打印数字来说太复杂了.

                  However, as it has been pointed in the comments, this approach is too complex for just printing numbers.

                  这里您可以找到很多关于 Tkinter 的信息和更多示例.

                  Here you can find a lot of information and more examples about Tkinter.

                  由于您的新问题已关闭,我将在此处更改代码以澄清最后一点.

                  Since your new question has been closed, I'll change the code here to clarify the last point.

                  这篇关于Python tkinter 中的多处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:python multiprocessing .join() 死锁依赖于worker函数 下一篇:生成器“TypeError:‘生成器’对象不是迭代器&qu

                  相关文章

                  最新文章

                1. <small id='ao3gT'></small><noframes id='ao3gT'>

                2. <i id='ao3gT'><tr id='ao3gT'><dt id='ao3gT'><q id='ao3gT'><span id='ao3gT'><b id='ao3gT'><form id='ao3gT'><ins id='ao3gT'></ins><ul id='ao3gT'></ul><sub id='ao3gT'></sub></form><legend id='ao3gT'></legend><bdo id='ao3gT'><pre id='ao3gT'><center id='ao3gT'></center></pre></bdo></b><th id='ao3gT'></th></span></q></dt></tr></i><div id='ao3gT'><tfoot id='ao3gT'></tfoot><dl id='ao3gT'><fieldset id='ao3gT'></fieldset></dl></div>

                    <tfoot id='ao3gT'></tfoot>

                    <legend id='ao3gT'><style id='ao3gT'><dir id='ao3gT'><q id='ao3gT'></q></dir></style></legend>

                      • <bdo id='ao3gT'></bdo><ul id='ao3gT'></ul>