首页 > 解决方案 > 当我关闭主框架时,如何避免程序继续运行?

问题描述

我目前正在自己​​学习 wxPython 库。我想到了用一个可以打开一个子框架的主框架创建一个 GUI。

我知道我可以通过将两个框架编译成相同的代码来做到这一点,但是对于我的项目,我需要将它们分开。

我成功地管理了子框架的打开和关闭,但不幸的是,它在我的父框架中产生了一个新问题。

这是我的代码:

wx_practicing.py

import wx
import time
import wx_Practicing_child
import threading
import os
import sys

"""Class which defines my main frame."""

class MainWindow(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, "Test", wx.DefaultPosition,
        (1000,850), wx.DEFAULT_FRAME_STYLE, wx.FrameNameStr)

        # Click counter
        self.click = 0

        # Init of the opening variable which is set to 1 when a child frame is opened
        self.OpenButtonFlag = 0

        # Init of the frame child invoked by the parent frame
        self.child = wx_Practicing_child.MainWindow_child()
        self.child.label = "child"

        # Sizers
        sizer_hori = wx.BoxSizer(wx.HORIZONTAL)
        sizer_verti = wx.BoxSizer(wx.VERTICAL)

        # Init of the panel

        test_panel = PanelMainWindow(self)
        test_panel.SetSizer(sizer_verti)

        # Buttons declaration
        # Button to quit the main frame
        btn_quit = wx.Button(test_panel, label ="Quit")
        btn_quit.Bind(wx.EVT_BUTTON, self.OnQuit)
        sizer_verti.Add(btn_quit)

        # Button counting number of time you trigger it

        btn_counter = wx.Button(test_panel, label="Click counter")
        sizer_verti.Add(btn_counter)
        btn_counter.Bind(wx.EVT_LEFT_DOWN, self.OnCount)

        # Button opening the child frame

        btn_new_frame = wx.Button(test_panel, label = "Open new frame",
                                    pos=(100,100))

        btn_new_frame.Bind(wx.EVT_LEFT_DOWN, self.OnNewFrame)


        self.Bind(wx.EVT_CLOSE, self.OnClose)

        # Frame displaying
        self.Show()

    def OnClose(self, event):

        self.Destroy(True)

    # Method used to close the parent frame
    def OnQuit(self, event):
        self.Destroy()
        print("closed")


    # Method used to count number of click
    def OnCount(self, event):
        self.click +=1
        print(self.click)

    # Method calling wx_Practicing_child.py to open a child frame
    def OnNewFrame(self, event):
        if self.child.OpenButtonFlag == 0 :
            self.child = wx_Practicing_child.MainWindow_child()
            self.child.label = "child"
            print("Flag before",self.child.OpenButtonFlag)
            self.child.Show()
            print("new Frame opened")
            self.child.OpenButtonFlag = 1
        else :
            print("Frame already launched, close the previous one and retry")
        print("Flag after", self.child.OpenButtonFlag)


"""Class of the panel"""

class PanelMainWindow(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)

test = wx.App(False)
frame = MainWindow()

test.MainLoop()

和 wx_Practicing_child.py

import wx
import time


"""Classe définissant une frame (i.e la zone globale parente). Elle permet
de faire exister le panel et ses attributs."""

class MainWindow_child(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, "Test", wx.DefaultPosition,
        (1000,850), wx.DEFAULT_FRAME_STYLE, wx.FrameNameStr)

        self.OpenButtonFlag = 0
        self.label = "Child"

        # Sizers
        sizer_hori = wx.BoxSizer(wx.HORIZONTAL)
        sizer_verti = wx.BoxSizer(wx.VERTICAL)

        # Init of the panel

        test_panel_child = PanelMainWindow_child(self)
        test_panel_child.SetSizer(sizer_verti)

        # Buttons declaration
        # Button to quit the frame
        btn_quit = wx.Button(test_panel_child, label ="Quit")
        btn_quit.Bind(wx.EVT_LEFT_DOWN, self.OnQuit)
        sizer_verti.Add(btn_quit)

    # Method used to quit the frame
    def OnQuit(self, event):
        self.OpenButtonFlag = 0
        self.Destroy()
        print("child print", self.OpenButtonFlag)



"""Class which defines a panel for the child frame"""

class PanelMainWindow_child(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)

所以我的主要问题是,当我点击父框架(wx_practicing.py)上的“退出”按钮或“x”框时,框架关闭,但程序并没有关闭。经过几次尝试,我注意到它似乎是由 MainWindow 中的 self.child 声明引起的。

但是,我需要此声明以允许 MainWindow 打开 MainWindow_child。我试图在我的 MainWindow 类的 Onquit() 方法中添加一个 self.child.Close() 但它没有成功。

经过一番研究,我发现我也许可以使用 CloseEvent 处理程序,但我并没有真正理解它的目的和它是如何工作的。

我希望我已经足够清楚了。

注意:这两个程序都在同一个文件夹中。

标签: python-3.xbuttonwxpythonframe

解决方案


欢迎来到 StackOverflow

您的代码的问题是您正在创建两个 MainWindow_child 实例。您可以通过在文件 wx_Practicing.py 中添加print(self.child)每一行来看到这一点。self.child = wx_Practicing_child.MainWindow_child()如果你现在运行你的代码并创建一个子框架,你会得到类似的东西:

<wx_Practicing_child.MainWindow_child object at 0x102e078b8>
<wx_Practicing_child.MainWindow_child object at 0x1073373a8>
Flag before 0
new Frame opened
Flag after 1

上面的前两行意味着您有两个 MainWindow_child 实例,并且指针self.child仅指向其中一个。因此,当您self.child.Destroy()以正常方式(红色 X 按钮)使用或关闭子框架时,您只会破坏一个实例。另一个实例永远存在,因为您从未显示它,因此您无法按下关闭按钮来销毁或使用它,self.child.Destroy()因为 self.child 指向不同的对象。永远活着的子框架是您的程序永远不会关闭的原因。

为了更清楚起见,如果您将wx_Practicing.py 中的OnQuit()和方法更改为:OnClose()

def OnClose(self, event):
    try:
        self.child.Destroy()
    except Exception:
        pass
    self.Destroy()

# Method used to close the parent frame
def OnQuit(self, event):
    try:
        self.child.Destroy()
    except Exception:
        pass
    self.Destroy()
    print("closed")

如果您不按下Open new frame按钮,程序将关闭,因为self.child指向子框架的唯一实例。按下按钮后,您将拥有两个实例,其中一个未显示且没有指针,并且程序不再关闭。


推荐阅读