0

私のコードでは、ボタンと背景が変わる場所の間に複数の画面が必要であると判断しました。これを行う最も簡単な方法は、パネルクラスを定義してから1つのフレームを作成することだと思いますが、私はしませんフレーム上のすべてのパネルを一緒にリンクする方法を知っています。各パネルに必要なボタンと画像はわかっていますが、パネルを定義し、ボタンをクリックしてそれらをリンクする方法がわかりません

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

1 に答える 1

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これで、「main」、「control」、または「rules」で呼び出すたびに、そのパネルが表示され、他のパネルは非表示になります。


switch_panelボタンクリックで呼び出すにはどうすればよいですか?

ボタンにイベント ハンドラーをバインドします。

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

my_button.bind(
    wx.EVT_BUTTON,
    lambda e: self.switch_panel('control')
)
于 2012-10-25T11:51:48.990 に答える