问题描述
我有 wxpython 应用程序,可以在单击按钮时打开 wx.DirDialog.
I have wxpython app that open wx.DirDialog on button click.
dlg = wx.DirDialog(self, "Choose a directory:", style=wx.DD_DEFAULT_STYLE)
if dlg.ShowModal() == wx.ID_OK:
# Do some stuff
由于我的应用程序是多线程的并且使用 wxTaskbaricon 允许用户(在 Win 7 上)关闭应用程序,即使模式 DirDialog 打开,我想在关闭主应用程序之前关闭 DirDialog.不知何故,以下方法不起作用:
Since my application is multithreaded and uses wxTaskbaricon which allow user (on Win 7) to close application even when modal DirDialog is open, I want to close the DirDialog before closing main app. Somehow non of below method work:
dlg.Destroy()
dlg.Close(True)
推荐答案
这是我的测试代码.
我可以在模态和非模态 wx.DirDialog 上测试
Destroy()
、Close()
和 EndModal()
()
I can test Destroy()
, Close()
and EndModal()
on Modal and Non-Modal wx.DirDialog()
要关闭模态对话框,我必须使用 Timer - 因为模态对话框阻止了对主窗口的访问.
To close modal dialog I had to use Timer - because modal dialog is blocking access to the main window.
只有当我这样做时它才能关闭对话框
It can't close dialog only if I do
self.dlg = None
self.dlg.EndModal(wx.CANCEL) # or Destroy() or Close(True)
还有一件事 - 我使用 Linux Mint 15、Python 2.7.4、wxPython 2.8.12.1 :)
And one more thing - I use Linux Mint 15, Python 2.7.4, wxPython 2.8.12.1 :)
#!/usr/bin/env python
#-*- coding: utf-8 -*-
import wx
import sys # to get python version
#----------------------------------------------------------------------
class MyFrame(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, title=title, size=(600,100))
self.panel = wx.Panel(self)
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.panel.SetSizer(self.sizer)
self.label = wx.StaticText(self.panel, label="Python "+sys.version+"\nwxPython"+wx.version())
self.button1 = wx.Button(self.panel, label="On")
self.button2 = wx.Button(self.panel, label="Off")
self.sizer.Add(self.label)
self.sizer.Add(self.button1)
self.sizer.Add(self.button2)
self.Bind(wx.EVT_BUTTON, self.OpenDialog, self.button1)
self.Bind(wx.EVT_BUTTON, self.CloseDialog, self.button2)
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.TimerCloseDialog, self.timer)
self.Show(True)
self.dlg = None
def OpenDialog(self, event):
print "OpenDialog"
self.timer.Start(3000, oneShot=True)
print "wait 3s ..."
if not self.dlg:
self.dlg = wx.DirDialog(self)
self.dlg.ShowModal()
#self.dlg.Show(True)
def CloseDialog(self, event):
print "CloseDialog"
if self.dlg:
#self.dlg = None
#self.dlg.EndModal(wx.CANCEL)
self.dlg.Destroy()
#self.dlg.Close(True)
def TimerCloseDialog(self, event):
print "TimerCloseDialog"
if self.dlg:
#self.dlg = None
self.dlg.EndModal(wx.CANCEL)
#self.dlg.Destroy()
#self.dlg.Close(True)
#----------------------------------------------------------------------
print "Python", sys.version
print "wxPython", wx.version()
app = wx.App()
frame = MyFrame(None, "Hello Dialog")
app.MainLoop()
这篇关于如何以编程方式关闭 wx.DirDialog?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!