【问题标题】:Creating a sub-process opens unwanted window创建子进程会打开不需要的窗口
【发布时间】:2017-07-27 02:17:29
【问题描述】:

我目前正在开发一个 Windows 应用程序,该应用程序需要使用我创建的 .exe 文件(在下面的示例中称为 kill_game.exe)。此 .exe 文件接受序列化对象作为参数。我使用subprocess.Popen 调用这个.exe 文件。当我这样做时,我当前窗口的精确副本将打开,并且我的 .exe 文件将不会运行,直到我自己关闭第二个窗口。我完全不知道为什么会这样。我希望 .exe 文件在不打开第二个 GUI 的情况下运行。我尝试切换到multiprocessing,因为它存在于this example on SO,但这也不起作用。

下面,我将发布我的代码的简化版本(我的原始版本是 300 多行),说明 GUI 和用于打开 .exe 文件的函数。我还将发布按下按钮时发生的情况的图片。

from tkinter import *
from tkinter import Tk
import subprocess

root = Tk()

sizex = sizey = 500
posx = posy = 100
root.wm_geometry("%dx%d+%d+%d" % (sizex, sizey, posx, posy))

myframe = Frame(root, relief=GROOVE, width=500, height=500, bd=1)
myframe.place(x=0, y=0)

canvas = Canvas(myframe)
frame = Frame(canvas)
canvas.pack(side="left")
canvas.create_window((0, 0), window=frame, anchor='nw')

button = Button(frame, text="Hello World")
button.grid(row=0, column=2)

games_to_stop = []
button.bind("<Button>",
            lambda event, game_list=games_to_stop: some_function(game_list))

def some_function(game_list):
    #This function is the function of interest, which is bound to my "Hello
    #World" Button
    child_script = r"C:\Users\Willy\AddictKiller\dist\kill_game.exe"
    subprocess.Popen([child_script, game_list])

root.mainloop()

这是按下按钮之前的 GUI:

这是按下按钮后的 GUI 和第二个 GUI(由于某种原因,它看起来像我的非简化代码的 GUI):

【问题讨论】:

  • 如果你替换 child_script = "notepad.exe" 会发生什么?
  • 您是通过 pythonw.exe 将 Tk 应用程序作为脚本运行,还是冻结的可执行文件?
  • @eryksun 如果我将其替换为 notepad.exe,则会弹出记事本,并会弹出一个对话框询问我是否要创建一个新的 .txt 文件
  • @eryksun 我不确定两者之间的区别,我相信pythonw.exe。什么是冻结的可执行文件?谷歌搜索并没有帮助我找到答案。
  • 在运行 notepad.exe 而不是 kill_game.exe 时,您是否获得了 Tk GUI 的第二个副本?

标签: python windows user-interface tkinter subprocess


【解决方案1】:

我有一个类似的问题,当我运行我的程序时,我的主 Tkinter 窗口的副本会打开,并且在我关闭所有副本之前该函数不会运行。 您可以通过将创建 tkiner 窗口的部分放在 if 语句后面来解决此问题,以确保您只在主窗口中创建一个窗口

    if __name__ == "__main__":
        root = Tk()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-06
    • 2019-03-17
    • 1970-01-01
    • 2010-10-24
    • 2012-06-12
    相关资源
    最近更新 更多