正确的方法独立运行两个TK()Mainloops,第二个脚本是从第一个脚本开始的?

发布于 2025-01-21 11:07:39 字数 824 浏览 0 评论 0原文

script_a.py

from tkinter import *
from script_b import test

root = Tk()

var1 = stuff
var2 = stuff

def start_scriptb():
    test([var1, var2])

Button(root, text="Start", 
command=start_scriptb)


root.mainloop()

script_b.py

from tkinter import *


def test(x):
    main = Tk()
    Label(main, text=x)
    Button(main, text="Exit", command=main.destroy())

    main.mainloop() 

这是我要实现的非常基本的版本。实际上,我正在卵形窗口中使用subprocess.popen中的一个进度窗口,其中脚本2中的popen通过了脚本一号的变量,并通过第二个程序上的滚动文本小部件查看了进程。我试图在每次从script_a命中该按钮时独立于root gui产生一个新过程。

到目前为止,它在我的测试中正常工作,但是我想看看这是否可能引起任何问题,还是实际上是在产生了两个过程?

我知道每个进程应该只有一个tk()

使用toplevel()窗口显示进度与线程模块可以正常工作,但是toplevel()窗口将冻结和root(以及任何其他开放式Toplevels( ))如果root正在进行任何需要任何时间的处理。

script_a.py

from tkinter import *
from script_b import test

root = Tk()

var1 = stuff
var2 = stuff

def start_scriptb():
    test([var1, var2])

Button(root, text="Start", 
command=start_scriptb)


root.mainloop()

script_b.py

from tkinter import *


def test(x):
    main = Tk()
    Label(main, text=x)
    Button(main, text="Exit", command=main.destroy())

    main.mainloop() 

This is a very basic version of what I'm trying to achieve. I actually am spawning a progress window that uses subprocess.Popen in script 2 with the passed through variables from script one and viewing the progess through a scrolled text widget on the 2nd program. I'm trying to spawn a new process, independent from the root GUI each time that button is hit from script_a.

It works fine in my tests so far, but I wanted to see if this could cause any issues or if this is actually spawning two processes?

I know there should only be one Tk() per process.

Using a TopLevel() window to show the progress works fine with the threading module, but the TopLevel() window will freeze as well as root (and any other open TopLevels()) if root is doing any sort of processing that takes any length of time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤寂小茶 2025-01-28 11:07:39

经过许多小时的测试,我确实在运行两个TK()循环方面取得了成功,但是它可能会出现问题,因为“ Bryan Oakley”在许多线程中发布了一些问题。

最终,我决定何时需要独自运行某些东西,我将以争论开始我的GUI,然后以一个全新的过程进行处理,而不是直接传递任何参数。似乎是一个更安全的选择。

With many hours of testing, I did have success in running two Tk() loops, but it had potential to be problematic, as "Bryan Oakley" had posted in many threads about.

Ultimately, I decided when I was in need of running something alone, I'd start my GUI with arguments and process it in an entirely new process instead of passing any arguments directly. Seems like a safer option.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文