【发布时间】:2019-11-01 23:22:23
【问题描述】:
我目前正在自学 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.x button wxpython frame