2013-11-15 171 views
0

好的,所以我在玩耍並試圖理解如何使用按鈕製作圖形用戶界面,我想我會從簡單的開始,讓兩個按鈕顯示一個一個不同的消息取決於哪一個被點擊。我做了第一個按鈕,並測試它...工作正常,做了第二個按鈕,當我測試它時,我收到了第二個按鈕的消息,當我點擊第一個按鈕時,沒有什麼時候我點擊第二個按鈕。我試着四處搜尋,但似乎沒有其他人有這個問題,所以我顯然做錯了什麼。wx按鈕點擊時只點擊一個對話框顯示對話框

#!/usr/bin/env python 
import os 
import wx 

class Frame(wx.Frame): 

    def OnOpen(self,e): 
     self.dirname='' 
     dlg=wx.FileDialog(self,'Choose a File',self.dirname,'','*.*',wx.OPEN) 

     if dlg.ShowModal()==wx.OK: 
      self.filename=dlg.GetFileName() 
      self.dirname=dlg.GetDirectory() 
      f=open(os.path.join(self.dirname,self.filename),'r') 
      self.Control.SetValue(f.read()) 
      f.close() 
      dlg.Destroy() 

    def OnAbout(self,e): 
     dlg=wx.MessageDialog(self,'Aoxx','Author',wx.OK) 
     dlg.ShowModal() 
     dlg.Destroy() 

    def OnExit(self,e): 
     dlg=wx.MessageDialog(self,'Exit','Terminate',wx.OK) 
     dlg.ShowModal() 
     self.Close(True) 
     dlg.Destroy() 

    def __init__(self,parent,id): 
     wx.Frame.__init__(self,parent,id,'Frame works',size=(450,600)) 
     panel=wx.Panel(self) 

     self.CreateStatusBar() 

     filemenu=wx.Menu() 
     self.filemenu=wx.Menu() 
     menubar=wx.MenuBar() 
     menubar.Append(filemenu,'&File') 
     #menubar.Append(filemenu,'&Help') 
     self.SetMenuBar(menubar) 

     MenuOpen=filemenu.Append(wx.ID_OPEN,'&Open','File Dir') 
     MenuExit=filemenu.Append(wx.ID_ANY,'E&xit','Term') 
     MenuAbout=filemenu.Append(wx.ID_ABOUT,'&About','Info') 

     self.Bind(wx.EVT_MENU,self.OnOpen,MenuOpen) 
     self.Bind(wx.EVT_MENU,self.OnExit,MenuExit) 
     self.Bind(wx.EVT_MENU,self.OnAbout,MenuAbout) 



     pic1=wx.Image('C:\Users\******\Pictures\Tri.bmp', wx.BITMAP_TYPE_BMP).ConvertToBitmap() 
     self.button=wx.BitmapButton(panel,-1, pic1,pos=(10,10)) 
     self.Bind(wx.EVT_BUTTON,self.ClickTri,self.button) 
     self.button.SetDefault() 

     pic2=wx.Image('C:\Users\******\Pictures\ClickWin.bmp', wx.BITMAP_TYPE_BMP).ConvertToBitmap() 
     self.buton=wx.BitmapButton(panel,-1,pic2,pos=(220,10)) 
     self.Bind(wx.EVT_BUTTON,self.ClickWin,self.button) 


    def ClickTri(self,event): 
     dlg=wx.MessageDialog(self,'No touching the TriForce Rook!','HEY!!!',wx.OK) 
     dlg.ShowModal() 
     dlg.Destroy()  

    def ClickWin(self,event): 
     dlg=wx.MessageDialog(self,'You would.....','REALLY?',wx.OK) 
     dlg.ShowModal() 
     dlg.Destroy() 

     self.Show(True) 

if __name__=='__main__': 
    app=wx.PySimpleApp() 
    frame=Frame(None,id=-1) 
    frame.Show() 
    app.MainLoop() 

回答

1

你不能有2 self.button使第二個self.button2什麼

+0

不錯!謝謝你,菜鳥的錯誤 – Aoxx

+0

如果我想按鈕打開一個新的框架,我需要在那個下面定義另一個類? exp 'def ClickTri(self,event): class Frame2(wx.Frame): ....' – Aoxx

+0

它取決於你想要的東西......你可能需要一個定義在某處的新框架......雖然不是你會做點擊事件 'def OnClick(self,evt):f2 = SomeOtherFrame(); f2.Show()' –

相關問題