Frame doesn't trigger EVT_ICONIZE

Post here if you need help with creating a Graphical User Interface in Python.

Frame doesn't trigger EVT_ICONIZE

Postby yueeng » Sun Apr 20, 2014 2:34 pm

Hello everyone, I'm programming a little wxpython program for set computer to hibernate(prefered this to shutdown) on time, all is set, but i just can't make it minimize to system tray and that's really annoying...

So I googled a lot about why the minimize button just don't trigger EVT_ICONIZE, but all i got was tutorials like "How to minimize to system tray". Of course it all gives the same solution as I implemented, which is
Code: Select all
self.Bind(EVT_ICONIZE,self.OnIconize)


I'm pretty sure that the problem is not the OnIconize function, because I tried to bind the EVT_CLOSE to it, and it worked just fine. So the it must be the trigger-process, right?

Here's the full code, you can easily locate the minimize function and the binding code:
Code: Select all
import wx
import wx.lib.agw.toasterbox as TB
import os
import time

class TaskBarIcon(wx.TaskBarIcon):
   def __init__(self,frame):
      wx.TaskBarIcon.__init__(self)
      self.frame=frame
      self.SetIcon(wx.Icon('icon.png',wx.BITMAP_TYPE_PNG),'Hibernator')
      self.Bind(wx.EVT_MENU,self.OnTaskBarClose,id=1)
      self.Bind(wx.EVT_TASKBAR_LEFT_DCLICK,self.OnTaskBarActive)
   
   def CreatePopupMenu(self):
      menu=wx.Menu()
      menu.Append(1,'Close')
      return menu
   def OnTaskBarActive(self,e):
      if not self.frame.IsShown():
         self.frame.Show()
         self.frame.Restore()
   def OnTaskBarClose(self,e):
      self.frame.Close()

class MainWindow(wx.Frame):
   def __init__(self,parent,id,title):
      wx.Frame.__init__(self,parent,id,title,size=(400,300),style=wx.SYSTEM_MENU|wx.CAPTION|wx.MINIMIZE_BOX|wx.CLOSE_BOX)

      self.InitUI()
      self.tskic=TaskBarIcon(self)
      self.bUser_warned=False

      self.timer=wx.Timer(self)
      self.Bind(wx.EVT_TIMER,self.OnUpdate,self.timer)
      self.Bind(wx.EVT_CLOSE,self.OnClose)
      self.Bind(wx.EVT_ICONIZE,self.OnMinimize)

      self.timer.Start()
      self.PopupMsg('Hey whats up man?')
      return
   def InitUI(self):
      '''Set menu bar'''
      menubar=wx.MenuBar()
      file_menu=wx.Menu()
      fm_item=file_menu.Append(wx.ID_ABOUT,'About','See more information' )
      menubar.Append(file_menu,'&File')
      self.SetMenuBar(menubar)
      self.Bind(wx.EVT_MENU,self.OnAbout,fm_item)

      '''set main frame'''
      # calculate the default time
      localtime=time.localtime(time.time())
      start_h=(localtime.tm_hour+1)%24
      start_m=localtime.tm_min

      pnl=wx.Panel(self)
      font=wx.Font(15,wx.DEFAULT,wx.NORMAL,wx.BOLD)

      vbox=wx.BoxSizer(wx.VERTICAL)
      self.info=wx.StaticText(pnl,label='SELECT',style=wx.ALIGN_CENTRE)
      self.info.SetFont(font)
      vbox.Add(self.info,proportion=1,flag=wx.ALIGN_CENTRE|wx.TOP,border=30)
      hbox=wx.BoxSizer(wx.HORIZONTAL)
      self.hourtext=wx.StaticText(pnl,label='Time ',style=wx.ALIGN_LEFT)
      self.hourtext.SetFont(font)
      hbox.Add(self.hourtext,proportion=1,flag=wx.ALIGN_CENTRE|wx.ALL,border=10)
      self.hour=wx.SpinCtrl(pnl,value=str(start_h),size=(60,-1),min=0,max=23,style=wx.SP_ARROW_KEYS|wx.SP_WRAP)
      hbox.Add(self.hour,proportion=1,flag=wx.ALIGN_CENTRE)
      self.seperator=wx.StaticText(pnl,label=":",style=wx.ALIGN_CENTRE)
      self.seperator.SetFont(font)
      hbox.Add(self.seperator,proportion=1,flag=wx.ALIGN_CENTRE|wx.ALL,border=10)
      self.minute=wx.SpinCtrl(pnl,value=str(start_m),size=(60,-1),min=0,max=59,style=wx.SP_ARROW_KEYS|wx.SP_WRAP)
      hbox.Add(self.minute,proportion=1,flag=wx.ALIGN_CENTRE)
      vbox.Add(hbox,proportion=1,flag=wx.ALIGN_CENTRE|wx.ALL,border=10)

      self.button=wx.ToggleButton(pnl,label='OK',size=(100,50))
      self.Bind(wx.EVT_TOGGLEBUTTON,self.OnClick,self.button)
      vbox.Add(self.button,proportion=0,flag=wx.ALIGN_CENTRE|wx.BOTTOM,border=40)

      pnl.SetSizer(vbox)
      self.SetTitle("Hibernator @lux")
      self.Centre()
      self.Show(True)
      return True
   def OnClose(self,e):
      self.tskic.Destroy()
      self.Destroy()

   def OnMinimize(self,e):
      self.Hide()

   def SetLockState(self):
      self.hour.Disable()
      self.minute.Disable()
      self.button.SetLabel("Cancel")
      self.info.SetLabel("LOCKED")
      return
   def SetInitState(self):
      self.hour.Enable()
      self.minute.Enable()
      self.button.SetLabel("OK")
      self.info.SetLabel("SELECT")
      self.bUser_warned=False
      return

   def OnClick(self,e):
      '''user confirmed schedule'''
      if self.button.GetValue()==True:
         self.SetLockState()
         return
      '''user canceled schedule'''
      if self.button.GetValue()==False:
         self.SetInitState()
         return

   def GetDeltaTime(self,hour,minute):
      now=time.localtime(time.time())
      dh=hour-now.tm_hour
      dm=minute-now.tm_min
      return dh*60+dm

   def OnUpdate(self,e):
      if self.button.GetValue():
         hour=self.hour.GetValue()
         hour=int(hour)
         minute=self.minute.GetValue()
         minute=int(minute)
         now=time.localtime(time.time())
         dtime=self.GetDeltaTime(hour,minute)
         if dtime>=0 and dtime<=5 and not self.bUser_warned:
            self.PopupMsg("System will hibernate in "+str(dtime)+" minutes.")
            self.bUser_warned=True

         if dtime==0:
            self.Restore()
            self.SetInitState()
            os.system("rundll32.exe Powrprof.dll,SetSuspendState")
      return True

   def OnAbout(self,e):
      dlg=wx.MessageDialog(None,'Author: lux\nEmail: y.heng7@gmail.com\nBlog: luxpark.farbox.com')
      dlg.ShowModal()
      return True

   def PopupMsg(self,msg,time=10000):
      sizex=200
      sizey=100
      toaster=TB.ToasterBox(self,tbstyle=TB.TB_SIMPLE)
      toaster.SetPopupText(msg)
      toaster.SetPopupSize((sizex,sizey))
      toaster.SetPopupPauseTime(time)
      tbpanel=toaster.GetToasterBoxWindow()
      dw,dh=wx.DisplaySize()
      x=dw-1-sizex
      y=dh-45-sizey
      toaster.SetPopupPosition((x,y))
      toaster.Play()




def main():
   app=wx.App()
   MainWindow(None,-1,'title')
   app.MainLoop()
   return True

if __name__ == '__main__':
   main()
Last edited by Mekire on Sun Apr 20, 2014 2:36 pm, edited 1 time in total.
Reason: First post lock. Title edited to use "doesn't" instead of "don't".
yueeng
 
Posts: 1
Joined: Sun Apr 20, 2014 2:22 pm

Return to GUI

Who is online

Users browsing this forum: silas247 and 1 guest