2012-10-25 4 views
0

내 코드를 사용하면 단추와 배경이 바뀌는 곳으로 갈 여러 화면이 필요하다고 결정했습니다. 내가 할 수있는 가장 쉬운 방법은 패널 클래스를 정의하는 것입니다. 한 프레임을 만들지 만 모든 프레임을 프레임에 연결하는 방법을 모르겠습니다. 나는 각 패널에서 원하는하지만 난이 여전히패널 클래스와 그 사이를 전환

작동하지 않습니다 내 새로운 코드

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

답변

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') 
) 
+0

어떻게하면 버튼을 할당 할 수 있습니까? –

+0

@MatthewRhysJones : 버튼 지정이란 무엇입니까? – Hubro

+0

내 인터페이스에는 기본 화면에 비트 맵 버튼이 있습니다. 게임 플레이 (스크립트에 링크), 규칙 (패널 변경) 및 컨트롤 (패널 변경)을 클릭 한 다음 전체 프레임을 닫을 때 종료합니다. 언급 한 기능을 할당하거나 클릭하여 단추로 작동하도록 수정하는 방법 –

관련 문제