0

我在 python 脚本中有 2 帧。一旦第一个最小化,第二个就会显示。关闭第二个后,如何使原始的从最小化到恢复。

到目前为止,这是我的代码:

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()
4

2 回答 2

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

可能有用......这就是我们使用的

于 2012-07-18T04:07:46.030 回答
1

您应该使用 myFrameObject.Raise() 使其脱离最小化状态。wx.STAY_ON_TOP 标志用于使框架在未最小化时保持在所有其他框架的顶部,但如果您已最小化框架,它将不会做任何事情。

于 2012-07-18T17:33:44.203 回答