面板类和它们之间的切换

时间:2012-10-25 10:40:53

标签: python wxpython

通过我的代码,我现在已经决定在按钮和背景改变的位置之间需要多个屏幕 我认为最简单的方法是通过定义面板类,然后创建一个框架,但我不知道如何在框架上将所有面板链接在一起。我知道每个面板上我想要哪些按钮和图像,但我不知道你如何定义面板并通过点击按钮链接它们

import os
import pygame
import wx
import os
import game

class MainPanel(wx.Panel):
    def __init__(self,parent,id):
        image_file='main_screen.jpg'#loading an image file from the folder
        bmp=wx.Bitmap(image_file)
        self.bitmap = wx.StaticBitmap(self, wx.ID_ANY, bmp, (0, 0))

        PlayButton=wx.Bitmap('play.jpg', wx.BITMAP_TYPE_ANY)
        self.PlayButton=wx.BitmapButton(self.bitmap, -1, PlayButton, pos=(190,300))
        self.PlayButton.Bind=(wx.EVT_BUTTON, self.opengame)

        RulesButton=wx.Bitmap('rules.jpg', wx.BITMAP_TYPE_ANY)
        self.RulesButton=wx.BitmapButton(self.bitmap, -1, RulesButton, pos=(190,370))
        self.RulesButton.Bind=(wx.EVT_BUTTON, self.openrules)

        ControlsButton=wx.Bitmap('controls.jpg', wx.BITMAP_TYPE_ANY)
        self.ControlsButton=wx.BitmapButton(self.bitmap, -1, ControlsButton, pos=(190,440))
        #self.ControlsButton.Bind=(wx.EVT_BUTTON, self.closeMe)

        ExitButton=wx.Bitmap('exit.jpg', wx.BITMAP_TYPE_ANY)
        self.ExitButton=wx.BitmapButton(self.bitmap,-1,ExitButton,pos=(190,510))
        self.ExitButton.Bind(wx.EVT_BUTTON, self.closeexit) 

        self.Bind(wx.EVT_CLOSE, self.closewindow)

class ControlPanel(wx.Panel):
    def __init__(self,parent,id):
        wx.Panel.__init__(self, parent, id=wx.ID_ANY)
        image_file='controls.jpg'#loading an image file from the folder
        bmp=wx.Bitmap(image_file)
        self.bitmap2 = wx.StaticBitmap(self, wx.ID_ANY, bmp, (0, 0))

        BackButton=wx.Bitmap('back.jpg',wx.BITMAP_TYPE_ANY)
        self.BackButton=wx.BitmapButton(self.bitmap2,-1,BackButton, pos=400,100)
        self.BackButton.Bind=(wx.EVT_BUTTON, self.goback)

class RulesPanel(wx.Panel):
    def __init__(self,parent,id):
        wx.Panel.__init__(self, parent, id=wx.ID_ANY)
        image_file='rules.jpg'#loading an image file from the folder
        bmp=wx.Bitmap(image_file)
        self.bitmap3 = wx.StaticBitmap(self, wx.ID_ANY, bmp, (0, 0))

        BackButton=wx.Bitmap('back.jpg',wx.BITMAP_TYPE_ANY)
        self.BackButton=wx.BitmapButton(self.bitmap3,-1,BackButton, pos=400,100)
        self.BackButton.Bind=(wx.EVT_BUTTON, self.goback)

class MainFrame(wx.Frame):
    def __init__(self,parent,id):
        wx.Frame.__init__(self,parent,id,'Compsci Vs. Sheep: The Game',size=(640,640))

    def openrules(self,event):


    def opengame(self):
    game.start()

    def opencontrols(self,event):
    ?
    def goback(self,event):
    ?
    def closewindow(self,event):
    self.Destroy()
    pygame.mixer.quit()
def closeexit

if __name__=='__main__':
    pygame.mixer.init(frequency=22050, size=-16, channels=2, buffer=4096)
    pygame.mixer.music.load("exorcist.ogg")
    pygame.mixer.music.play(-1)#music playing in program
    app=wx.PySimpleApp()
    frame=menu(parent=None,id=-1)
    frame.Show()#shows the screen
    app.MainLoop()

这是我的新代码仍无效

import os
import pygame
import wx

def switch_to(name):
    print "Pseudo switch",name



class MainFrame(wx.Frame):
    def __init__(self,parent,id):
        wx.Frame.__init__(self,parent,id,'Compsci Vs. Sheep: The Game',size=(640,640))

        self.box = wx.BoxSizer()

        self._panels = {}
        self._panels['main'] = MainPanel(self, -1)
        self._panels['rules'] = RulesPanel(self, -1)
        self._panels['rules'].Hide()

        self.box.Add(self._panels['main'],1,wx.EXPAND)
        self.box.Add(self._panels['rules'],1,wx.EXPAND)

        self.SetSizer(self.box)

    def switch_panel(self, name):
        print "Switching to",name
        return
        for key, panel in self._panels.iteritems():
            if key != name:
                panel.Hide()
            else:
                panel.Show(True)        
        self.Layout()

class MainPanel(wx.Panel):
    def __init__(self,parent,id):
        wx.Panel.__init__(self,parent,id=wx.ID_ANY)
        image_file='main_screen.jpg'#loading an image file from the folder
        bmp=wx.Bitmap(image_file)
        self.bitmap = wx.StaticBitmap(self, wx.ID_ANY, bmp, (0, 0))

        PlayButton=wx.Bitmap('play.jpg', wx.BITMAP_TYPE_ANY)
        self.PlayButton=wx.BitmapButton(self,-1, PlayButton, (190,300), (244,60))

        RulesButton=wx.Bitmap('rules.jpg', wx.BITMAP_TYPE_ANY)
        self.RulesButton=wx.BitmapButton(self, -1, RulesButton, (190,300), (244,60))
        self.RulesButton.Bind=(wx.EVT_BUTTON, parent.switch_panel)

class RulesPanel(wx.Panel):
    def __init__(self,parent,id):
        wx.Panel.__init__(self, parent, id=wx.ID_ANY)
        image_file='rules.jpg'#loading an image file from the folder
        bmp=wx.Bitmap(image_file)
        self.bitmap = wx.StaticBitmap(self, wx.ID_ANY, bmp, (0, 0))

if __name__=='__main__':
    pygame.mixer.init(frequency=22050, size=-16, channels=2, buffer=4096)
    pygame.mixer.music.load("exorcist.ogg")
    pygame.mixer.music.play(-1)#music playing in program
    app = wx.PySimpleApp()
    frame = MainFrame(parent=None,id=-1)
    frame.Show()#shows the screen
    app.MainLoop()

1 个答案:

答案 0 :(得分:0)

您可以在框架构造函数中创建所有面板并将它们放在字典中。然后你可以创建一个名为switch_panel的函数,它隐藏除了你想要显示的面板之外的所有面板。例如:

def MyFrame(wx.Frame):
    # Dict for storing the panels.
    _panels = {}

    def __init__(self, parent, id):
        wx.Frame.__init__(self, parent, id, 'Example')

        # Create all the panels.
        self._panels['main'] = MainPanel(self, -1)
        self._panels['control'] = ControlPanel(self, -1)
        self._panels['rules'] = RulesPanel(self, -1)

        # Hide all the panels initially.
        for key, panel in self._panels.iteritems():
            panel.Hide()

        # Show the main panel.
        self.switch_panel('main')

    def switch_panel(self, name):
        """Function for switching between the frame's panels."""
        for key, panel in self._panels.iteritems():
            if key != name:
                panel.Hide()
            else:
                panel.Show(True)

现在,只要您使用“主要”,“控制”或“规则”致电switch_panel,就会显示该面板,其他面板将被隐藏。


如何在按钮上点击switch_panel

将事件处理程序绑定到按钮,例如

my_button = wx.Button(self, -1, 'Click me!')

my_button.bind(
    wx.EVT_BUTTON,
    lambda e: self.switch_panel('control')
)