lyxleo
2019-03-11 13:03
采纳率: 28.6%
浏览 2.6k
已采纳

如何关闭wxpython打开的窗口?

我先用wxpython打开一个窗口,但等到一定时间后自动关闭这个窗口 ,该怎么关闭?

import wx
class mywindow(wx.App):
    def __init__(self,parent,id):
        wx.Frame.__init__(self,parent,id,title="窗口",pos=(100,100),size=(500,500)

if __name__=='__main__':
    app=wx.App()
    frame=mywindow(parent=None,id=-1)
    frame.Show()
    app.MainLoop()

就是不明白如何关闭窗口,请大家帮帮忙!感谢!

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • 嗜血大魔神 2019-03-11 14:51
    已采纳

    sleep 休眠 几秒
    frame .close()

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • 躲在车里的猫 2019-03-11 15:11
    import wx
    import threading
    import time
    
    
    class mywindow(wx.App):
        def doClose(self,j):
            time.sleep(j)
            self.frame.Close()
        def __init__(self):
            wx.App.__init__(self)
            self.frame = wx.Frame(parent=None, id=-1, title="窗口", pos=(100, 100), size=(500,500))
            self.frame.Show()
            t =threading.Thread(target=self.doClose,args=(0.5,))
            t.start()
    if __name__ == '__main__':
        app = mywindow()
        app.MainLoop()
    
    评论
    解决 无用
    打赏 举报
  • 那小麦 2019-03-11 15:13

    窗口关闭事件

    现在有一个主窗口,然后加了一个TaskBarIcon
    第一:我想把主窗口中的关闭事件改成隐藏主窗口,不知道怎么实现,在网上搜了一圈找到了wx.CloseEvent这个好像和close事件有关的。。但是不知道如何操作。。

    第二:程序创建了一个Frame和一个TaskBarIcon,但是在关闭的时候有点问题。从Frame的关闭只能销毁Frame本身,而在TaskBarIcon中定义的菜单退出可以销毁所有资源
    新车上牌需要哪些手续和步骤http://www.danews.cc/car/xinche/2019/0311/2652.html

    代码:

    -*- coding: cp936 -*-

    import wx,sys
    from threading import Thread
    from helper import helpApp
    from UserData import UserInfo
    from udprotocol import MSGER

    sys config

    Default = helpApp()

    class mainWindow():

    def __init__(self, parent):
    
        # ---- init param -----
    
        User = UserInfo("192.168.1.47", "00-23-54-CA-83-F3")
        User = User.getUserInfo()
        self.USER_INFO = User[2] + "\nIP: " + User[0]
        self.USER_IMAGE = User[3]
    
    
    
    
        # ---- end -----
    
        MainPanel = wx.Panel(parent, -1)
    
        # activeX
        self.HOSTIMAGE = wx.StaticBitmap(MainPanel, -1, bitmap=self.ConvertUserImage())
    
        self.HOSTInfo = wx.StaticText(MainPanel, -1, self.USER_INFO)
    
        self.USERList = wx.TreeCtrl(MainPanel, -1)
    
        # user tree
        root = self.USERList.AddRoot("默认分组")
    
        # ---- layout -----
    
        HBox = wx.BoxSizer(wx.VERTICAL)
    
        # host sizer
        HOSTBox = wx.BoxSizer(wx.HORIZONTAL)
        HOSTBox.Add(self.HOSTIMAGE, 0)
        HOSTBox.Add(self.HOSTInfo, 0, wx.LEFT, 5)
    
        # list sizer
        ListBox = wx.BoxSizer(wx.HORIZONTAL)
        ListBox.Add(self.USERList, 1, wx.EXPAND)
    
        # order by row
        HBox.Add(HOSTBox, 0, wx.LEFT | wx.TOP, 5)
        HBox.Add((-1, 5))
        HBox.Add(ListBox, 1, wx.LEFT | wx.RIGHT | wx.BOTTOM | wx.EXPAND, 5)
    
        MainPanel.SetSizer(HBox)
    
        # ---- end layout -----
    
    
    def ConvertUserImage (self):
        """resize image"""
    
        TempIMAGE = wx.Image(self.USER_IMAGE, wx.BITMAP_TYPE_ANY)
        TempIMAGE_Width = TempIMAGE.GetWidth()
        TempIMAGE_Height = TempIMAGE.GetHeight()
    
        TempIMAGE_NewSize = Default.ThumbSize(TempIMAGE_Width, TempIMAGE_Height)
        TempIMAGE_Thumb = TempIMAGE.Scale(TempIMAGE_NewSize[0], TempIMAGE_NewSize[1])
        TempBMP = TempIMAGE_Thumb.ConvertToBitmap()
    
        return TempBMP
    
    def AddTreeNodes(self, parentItem, items):
        for item in items:
            if type(item) == str:
                newItem = self.tree.AppendItem(parentItem, item)
            else:
                newItem = self.tree.AppendItem(parentItem, item[0])
                self.AddTreeNodes(newItem, item[1])
    

    class TaskBarICO(wx.TaskBarIcon):
    TBMENU_SET = wx.NewId()
    TBMENU_CLOSE = wx.NewId()

    def __init__(self, parent):
        wx.TaskBarIcon.__init__(self)
        self.frame = parent
        self.SetIcon(wx.Icon(name = 'Msger.ico', type = wx.BITMAP_TYPE_ICO), Default.SYS_NAME + " " + Default.SYS_VERSION)
    
        self.Bind(wx.EVT_TASKBAR_LEFT_DCLICK, self.OnTaskBarLeftDClick)
        self.Bind(wx.EVT_MENU, self.OnSysSet, id = self.TBMENU_SET)
        self.Bind(wx.EVT_MENU, self.OnExit, id = self.TBMENU_CLOSE)
    
    def CreatePopupMenu(self):
        """
        This method is called by the base class when it needs to popup
        the menu for the default EVT_RIGHT_DOWN event.
        """
        menu = wx.Menu()
        menu.Append(self.TBMENU_SET, "系统设置")
        menu.AppendSeparator()
        menu.Append(self.TBMENU_CLOSE, "退出")
        return menu
    
    def OnTaskBarLeftDClick(self, event):
        if self.frame.IsIconized():
           self.frame.Iconize(False)
        if not self.frame.IsShown():
           self.frame.Show(True)
        self.frame.Raise()
    
    def OnSysSet (self, event):
        pass
    
    def OnExit (self, event):
        sys.exit()
    

    class mainApp(wx.App):
    def init(self):
    wx.App.__init__(self, 0)
    return None

    def OnInit(self):
        """create main window"""
    
        self.frame = wx.Frame(parent = None, title = Default.SYS_NAME + " " + Default.SYS_VERSION, size = (Default.MainWindow_Default_Width,Default.MainWindow_Default_Height))
    
        self.MainWin = mainWindow(self.frame)
        self.TaskBar = TaskBarICO(self.frame)
    
        self.frame.SetIcon(wx.Icon('Msger.ico', wx.BITMAP_TYPE_ICO))
        self.frame.SetMinSize((Default.MainWindow_Min_Width,Default.MainWindow_Min_Height))
        self.frame.Center()
        self.frame.Show()
    
        #self.Bind(wx.EVT_CLOSE, self.OnClose, self.frame)
    
        return True
    
    def OnHide(self, event):
        self.frame.Hide()
    

    def OnClose (self, event):

    print "Close soon..."

    #wx.MessageBox('Hello From TaskBarIcon!', 'Prompt')

    self.frame.Show(False)

    return False

    #self.TaskBar.Close()

    #self.frame.Close()

    #sys.exit()

    #wx.CloseEvent.Veto()

    #

    #wx.CallAfter(TaskBarICO.OnExit)

    #self.ExitMainLoop()

    def OnExit (self):
        self.frame.Iconize(True)
        self.SetExitOnFrameDelete(False)
        print "Close soon..."
        return False
    

    if name == '__main__':

    App = mainApp()
    App.MainLoop()
    
    wx.CallAfter(MSGER())
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题