2012-07-18 84 views
0

我在Python腳本中有2個框架。一旦第一個最小化,第二個顯示。在關閉第二個之後,我怎樣才能讓原來的那個從最小化到恢復。如何在wxpython/python中恢復最小化窗口/框架

這是到目前爲止我的代碼:

import wx 
import wx.media 
import os 

class MainWindow(wx.Frame): 

    def __init__(self,parent,id): 
     wx.Frame.__init__(self,parent,id,'Music Player',size=(900,600), style=wx.MINIMIZE_BOX|wx.SYSTEM_MENU|wx.CAPTION|wx.CLOSE_BOX|wx.CLIP_CHILDREN) 
     wx.Frame.CenterOnScreen(self) 
     panel=wx.Panel(self) 
     panel2=wx.Panel(panel,-1, (0,0), (160,600)) 
     ##wx.Frame.Maximize(self) 
     panel.SetBackgroundColour('grey') 
     panel2.SetBackgroundColour('black') 

     ##MENU AND STATUS BAR 
     status=self.CreateStatusBar() 
     menubar=wx.MenuBar() 
     file_menu=wx.Menu() 
     help_menu=wx.Menu() 

     ID_FILE_NEW = 1 
     ID_FILE_LOAD = 2 
     ID_FILE_EXIT = 3 

     file_menu.Append(ID_FILE_NEW,"New Window","This will open a new window") 
     file_menu.Append(ID_FILE_LOAD,"Load...", "This will let you choose a song to load") 
     file_menu.Append(ID_FILE_EXIT,"Exit","This will exit the program") 

     menubar.Append(file_menu,"File") 
     self.SetMenuBar(menubar) 

     self.Bind(wx.EVT_MENU, self.newWin, None, 1) 
     self.Bind(wx.EVT_MENU, self.Load, None, 2)   
     self.Bind(wx.EVT_MENU, self.Close, None, 3) 

##  heading = wx.StaticText(panel, -1, "Music Player",pos=(345,10)) 
     font1 = wx.Font(14, wx.DEFAULT, wx.NORMAL, wx.BOLD) 
     font2 = wx.Font(14, wx.DEFAULT, wx.NORMAL, wx.BOLD) 
##  heading.SetFont(font1) 

     try: 
      self.mc = wx.media.MediaCtrl(self) 
     except NotImplementedError: 
      raise 

     loadButton = wx.Button(panel2, -1, "Load File", pos=(30,10)) 
     self.Bind(wx.EVT_BUTTON, self.Load, loadButton) 

     playButton = wx.Button(panel2, -1, "Play", pos=(30,50)) 
     self.Bind(wx.EVT_BUTTON, self.Play, playButton) 

     pauseButton = wx.Button(panel2, -1, "Pause", pos=(30,90)) 
     self.Bind(wx.EVT_BUTTON, self.Pause, pauseButton) 

     stopButton = wx.Button(panel2, -1, "Stop", pos=(30,130)) 
     self.Bind(wx.EVT_BUTTON, self.Stop, stopButton) 

     self.playbackSlider = wx.Slider(panel, size=(400,45), pos=(310,50)) 
     self.playbackSlider.SetRange(0,self.mc.Length()) 
     self.Bind(wx.EVT_SLIDER, self.onSeek, self.playbackSlider) 
     self.playbackSlider.SetBackgroundColour('grey') 

     self.volumeCtrl = wx.Slider(panel, value=50, minValue=0, maxValue=100, style=wx.SL_VERTICAL|wx.SL_INVERSE, pos=(180,40)) 
     self.volumeCtrl.Bind(wx.EVT_SLIDER, self.onSetVolume) 

     self.timer = wx.Timer(self) 
     self.Bind(wx.EVT_TIMER, self.onTimer) 
     self.timer.Start(100) 

     self.Bind(wx.EVT_ICONIZE, self.newWin2)   

     self.st_file = wx.StaticText(self, -1, "**Nothing** Please click the \"Load File\" Button", pos=(310,10)) 
     playing = wx.StaticText(self, -1, "Now Playing: ", pos=(165,10)) 
     playing.SetFont(font2) 
     self.st_file.SetFont(font1) 
     playing.SetBackgroundColour('grey') 
     self.st_file.SetBackgroundColour('grey') 
     playing.SetForegroundColour('white') 
     self.st_file.SetForegroundColour('white') 

    def newWin2(self, event): 
     if self.IsIconized() == True: 
      self.new = NewWindow(parent=None, id=-1) 
      self.new.Show() 

    def newWin(self, event): 
      self.new = NewWindow(parent=None, id=-1) 
      self.new.Show() 

    def Close(self, event): 
     box=wx.MessageDialog(None, 'Are you sure you want to exit?', 'Exit program?', wx.YES_NO) 
     answer=box.ShowModal() 
     if answer==wx.ID_YES: 
      self.Destroy() 

    def Load(self, event): 
     dlg = wx.FileDialog(self, "Choose a media file", "songs", "", "*.*", wx.OPEN) 
     if dlg.ShowModal() == wx.ID_OK: 
      path = dlg.GetPath() 
      self.doLoadFile(path) 
     dlg.Destroy() 

    def doLoadFile(self, path): 
     if not self.mc.Load(path): 
      wx.MessageBox("Unable to load %s: Unsupported format?" % path, "ERROR", wx.ICON_ERROR | wx.OK) 

     else: 
      folder, filename = os.path.split(path) 
      self.st_file.SetLabel('%s' % filename) 
      self.mc.SetBestFittingSize() 
      self.mc.Play() 

    def Play(self, event): 
     self.mc.Play() 
     self.playbackSlider.SetRange(0,self.mc.Length()) 

    def Pause(self, event): 
     self.mc.Pause() 

    def Stop(self, event): 
     self.mc.Stop() 

    def onSetVolume(self, event): 
     self.currentVolume = self.volumeCtrl.GetValue() 
     self.mc.SetVolume(float(self.currentVolume)/100) 

    def onTimer(self, event): 
     offset = self.mc.Tell() 
     self.playbackSlider.SetValue(offset) 

    def onSeek(self, event): 
     offset = self.playbackSlider.GetValue() 
     self.mc.Seek(offset) 

class NewWindow(wx.Frame): 

    def __init__(self,parent,id): 
     wx.Frame.__init__(self, parent, id, 'New Window', size=(130,150), pos=(0,0), style=wx.MINIMIZE_BOX|wx.CLOSE_BOX) 
     self.SetBackgroundColour('grey') 

     playButton = wx.Button(self, -1, "Play", pos=(5,10)) 
     self.Bind(wx.EVT_BUTTON, self.Play, playButton) 

     pauseButton = wx.Button(self, -1, "Pause", pos=(5,40)) 
     self.Bind(wx.EVT_BUTTON, self.Pause, pauseButton) 

     stopButton = wx.Button(self, -1, "Stop", pos=(5,70)) 
     self.Bind(wx.EVT_BUTTON, self.Stop, stopButton) 

     closeButton = wx.Button(self, -1, "Close", pos=(5,120)) 
     self.Bind(wx.EVT_BUTTON, self.Close, closeButton) 

    def Play(self, event): 
     self.mc.Play() 
     self.playbackSlider.SetRange(0,self.mc.Length()) 

    def Pause(self, event): 
     self.mc.Pause() 

    def Stop(self, event): 
     self.mc.Stop() 

    def onSetVolume(self, event): 
     self.currentVolume = self.volumeCtrl.GetValue() 
     self.mc.SetVolume(float(self.currentVolume)/100) 

    def Close(self, event): 
     self.Destroy() 
     ################## I WANT TO ADD WHATEVER I NEED TO HERE TO RESTORE THE MINIMIZED FRAME AFTER THE CLOSE BUTTON IS PRESSED. 

##RUN## 

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

回答

1
the_app.SetTopWindow(wxFrameObject) 
wxFrameObject.Maximize() 

可能工作......那是什麼我們使用

+0

當你說「the_app」時,你到底提到了什麼。一切都很有意義 – Worm 2012-07-18 04:09:09

+0

wx.App對象...此代碼被包裹在我們的項目中的一堆其他東西,所以它可能會或可能不會獨立工作 – 2012-07-18 04:10:33

+0

在旁邊注意帶代碼提示的IDE可能有助於解決此類問題也因爲它會告訴你最大化是一個wx.Frame的方法(真的是wx.TopLevelWindow或其他)(我使用eclipse)...你也可能想用'wx.STAY_ON_TOP'樣式初始化框架我添加了 – 2012-07-18 04:12:28

1

您應該使用myFrameObject.Raise(),使其來的最小的了州。 wx.STAY_ON_TOP標誌用於在幀未被最小化時使幀保持在所有其他幀之上,但是如果已經最小化幀,它將不會執行任何操作。

相關問題