繁体   English   中英

如何替换 subprocess.Popen(),在转换为 exe 后阻塞 Tkinter

[英]how to replace subprocess.Popen(), is blocking Tkinter after conversion to exe

部分代码:

class A:
    def __init__(self,my_arg):

      do sth...

    def f1(self):

       one = (sys.executable, "script.py")
       output = subprocess.Popen(one, stdout=subprocess.PIPE)

    def f2(self):

       do_sth...

class program(tk.Tk):
    def __init__(self):
        super().__init__()
        ...
        
        button_run  = Button(root)
        button_run.configure(command = self.run_program)
        root.mainloop()

    def run_program(self): 
        file_list = ['file1','file2']
        for file in file_list:
           cls_A = A(file)
           cls_A.f1()
           cls_A.f2()

if __name__== '__main__':
   program()

问题:

我已阅读有关此问题的许多主题,但没有看到任何解决方案。 子进程在转换为 exe 后阻止 Tkinter,并且我的程序与从脚本运行的程序不同,单击“运行”按钮后正在创建新的 tkinter window 并再次...

我知道这个问题与子进程部分有关,如何运行与子进程相同的外部模块,但不同?

此解决方案不起作用: pyInstaller 多次加载脚本

由于这条线而出现问题

one = (sys.executable, "script.py")

这已在此处的文档中提到

当一个普通的 Python 脚本运行时,sys.executable 是执行程序的路径,即 Python 解释器。 在冻结的应用程序中,sys.executable 也是已执行程序的路径,但不是 Python; 它是单文件应用程序中的引导加载程序或单文件夹应用程序中的可执行文件。

因此,您的 exe 将重新启动,并且脚本不会被执行。

您可以尝试使用import script将脚本作为模块导入,并使用 OOP 来简化执行。 如果脚本被阻塞,那么您可以使用threading在不同的线程中启动它,如果脚本需要在单独的主线程中执行,那么您可以为您的脚本创建另一个可执行文件并调用它(如果使用--onedir模式下,您可以创建一个 python 文件,该文件导入主脚本和其他脚本的依赖项并构建一个基本文件夹,稍后您可以在相同模式下创建 2 个 exe 并将它们放在公共基本目录中,总体上具有相对较小的大小)。

暂无
暂无

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

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