简体   繁体   English

如何在wxpython / python中恢复最小化的窗口/框架

[英]How to restore a minimized Window/Frame in wxpython/python

I have 2 frames in a python script. 我在python脚本中有2个帧。 Once the first one gets minimized, the second one shows. 一旦第一个被最小化,第二个显示。 How do I get the original one to go from being minimized to restored after I close the second one. 在关闭第二个之后,如何让原始的从最小化到恢复。

Here is my code so far: 到目前为止,这是我的代码:

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()
the_app.SetTopWindow(wxFrameObject)
wxFrameObject.Maximize()

might work ... thats what we use 可能会工作...这就是我们使用的东西

You should use myFrameObject.Raise() to make it come out of its minimized state. 您应该使用myFrameObject.Raise()使其退出最小化状态。 The wx.STAY_ON_TOP flag is for making the frame stay on top of all other frames when it's not minimized, but it won't do anything if you have minimized the frame. wx.STAY_ON_TOP标志用于使帧在未最小化时保持在所有其他帧的顶部,但如果已最小化帧,则它将不会执行任何操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM