为什么在关闭wxPython中的对话框时会出现一个小窗口?

时间:2014-11-19 23:33:58

标签: wxpython panels

我有wx.Frame来调用wx.Dialog

现在,他们都做了相应的工作并且开得很好。问题是,当他们这样做时,他们也打开一个小窗口"它位于我框架的左上角,在那之后我无法使用鼠标自动调整框架大小。关闭dialog没有任何效果,因为那个小窗口仍然在那里。

wx.Frame内,我有以下事件调用:

def OnTerminalSettings(self, event):
    """Open Terminal settings dialog"""
    dlg = TerminalSettingsDialog(self, wx.ID_ANY, "", settings=TerminalPanel(self).settings)
    dlg.ShowModal()
    dlg.Destroy()

TerminalSettingsDialog wx.Dialog看起来像这样:

class TerminalSettingsDialog(wx.Dialog):
    """Simple dialog with common terminal settings."""

    def __init__(self, *args, **kwds):
        self.settings = kwds['settings']
        del kwds['settings']
        kwds["style"] = wx.DEFAULT_DIALOG_STYLE
        wx.Dialog.__init__(self, *args, **kwds)
        self.checkbox_echo = wx.CheckBox(self, wx.ID_ANY, "Local Echo")
        self.checkbox_unprintable = wx.CheckBox(self, wx.ID_ANY, "Show unprintable characters")
        self.radio_box_newline = wx.RadioBox(self, wx.ID_ANY, "Newline Handling", choices=["CR only", "LF only", "CR+LF"], majorDimension=0, style=wx.RA_SPECIFY_ROWS)
        self.button_ok = wx.Button(self, wx.ID_ANY, "OK")
        self.button_cancel = wx.Button(self, wx.ID_ANY, "Cancel")

        self.__set_properties()
        self.__do_layout()

        self.__attach_events()
        self.checkbox_echo.SetValue(self.settings.echo)
        self.checkbox_unprintable.SetValue(self.settings.unprintable)
        self.radio_box_newline.SetSelection(self.settings.newline)

    def __set_properties(self):
        self.SetTitle("Terminal Settings")
        self.radio_box_newline.SetSelection(0)
        self.button_ok.SetDefault()

    def __do_layout(self):
        sizer_2 = wx.BoxSizer(wx.VERTICAL)
        sizer_3 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_4 = wx.StaticBoxSizer(wx.StaticBox(self, wx.ID_ANY, "Input/Output"), wx.VERTICAL)
        sizer_4.Add(self.checkbox_echo, 0, wx.ALL, 4)
        sizer_4.Add(self.checkbox_unprintable, 0, wx.ALL, 4)
        sizer_4.Add(self.radio_box_newline, 0, 0, 0)
        sizer_2.Add(sizer_4, 0, wx.EXPAND, 0)
        sizer_3.Add(self.button_ok, 0, 0, 0)
        sizer_3.Add(self.button_cancel, 0, 0, 0)
        sizer_2.Add(sizer_3, 0, wx.ALL|wx.ALIGN_RIGHT, 4)
        self.SetAutoLayout(1)
        self.SetSizer(sizer_2)
        sizer_2.Fit(self)
        sizer_2.SetSizeHints(self)
        self.Layout()

    def __attach_events(self):
        self.Bind(wx.EVT_BUTTON, self.OnOK, id = self.button_ok.GetId())
        self.Bind(wx.EVT_BUTTON, self.OnCancel, id = self.button_cancel.GetId())

    def OnOK(self, events):
        """Update data with new values and close dialog."""
        self.settings.echo = self.checkbox_echo.GetValue()
        self.settings.unprintable = self.checkbox_unprintable.GetValue()
        self.settings.newline = self.radio_box_newline.GetSelection()
        self.EndModal(wx.ID_OK)

    def OnCancel(self, events):
        """Do not update data, but rather close dialog."""
        self.EndModal(wx.ID_CANCEL)

我不知道这是否重要,但框架中有一个顶部wx.Panel,而wx.Panel又包含其他两个TerminalPanel。我希望能够在其中一个面板中安装一个终端。

我从PySerial examples获得了终端的代码,所以我猜他们的工作正常。

任何人都可以借给我一个手吗?

附录

这是class TerminalPanel(wx.Panel): def __init__(self, *args, **kwds): self.serial = serial.Serial() self.serial.timeout = 0.5 #make sure that the alive event can be checked from time to time self.settings = TerminalSetup() #Placeholder for the settings self.thread = None self.alive = threading.Event() kwds["style"] = wx.DEFAULT_FRAME_STYLE wx.Panel.__init__(self, *args, **kwds) TextPanel.text_ctrl_output = wx.TextCtrl(self, -1, "", style=wx.TE_MULTILINE) self.__do_layout() # self.OnPortSettings(None) #Call setup dialog on startup, opens port if not self.alive.isSet(): self.Close() def StartThread(self): """Start the receiver thread.""" self.thread = threading.Thread(target=self.ComPortThread) self.thread.setDaemon(1) self.alive.set() self.thread.start() def StopThread(self): """Stop the receiver thread, wait until it's finished.""" if self.thread is not None: self.alive.clear() #Clear alive event for thread self.thread.join() #Wait until thread has finished self.thread = None #Set flag as empty def __do_layout(self): """Set general layout """ sizer_1 = wx.BoxSizer(wx.VERTICAL) sizer_1.Add(self.text_ctrl_output, 1, wx.EXPAND, 0) self.SetAutoLayout(1) self.SetSizer(sizer_1) self.Layout() 内的代码:

{{1}}

1 个答案:

答案 0 :(得分:2)

dlg = TerminalSettingsDialog(self, wx.ID_ANY, "", settings=TerminalPanel(self).settings)

此语句正在创建TerminalPanel的新实例,每次创建对话框实例时,该实例都是self的子项。那是你额外的窗口。如果你已经有TerminalPanel某个地方的实例,那么你应该从中获取设置对象,而不是创建一个新实例。