wxpython 學習之--panel


一般控件是放在panel上的,當然控件也能放在Frame上,看下這兩者的區別:

1.button放在panel上:

#coding:utf-8
import wx
import os
class MyApp(wx.App):
    def __init__(self):
        super(MyApp,self).__init__()

class MyFrame(wx.Frame):
    def __init__(self,title='test',size=wx.DefaultSize):
        super(MyFrame,self).__init__(None,wx.ID_ANY,title=title,size=size,style=wx.DEFAULT_FRAME_STYLE^wx.MINIMIZE_BOX)
        self.Center()
        #self.SetSize(700,700)
        #self.SetTitle('aaa')
        self.InitMenuBar()
        self.InitStatusBar()
        self.panel=wx.Panel(self) #創建panel,大小和Frame是一樣大的
        button=wx.Button(self.panel,label=u'Start',size=(50,30),pos=(0,30)) #將button放在panel上

    def InitMenuBar(self):
        #創建一個menubar
        menuBar = wx.MenuBar()

        #創建兩個menu
        filemenu = wx.Menu()
        aboutmenu = wx.Menu()

        #filemenu添加一個menuopen,關聯的ID為wx.ID_OPEN,名字為Open,如果有狀態欄,則狀態欄里顯示‘打開文件’
        menuopen = filemenu.Append(wx.ID_OPEN,'Open','打開文件')
        #filemenu添加一個menu分隔符
        filemenu.AppendSeparator()
        menusave = filemenu.Append(wx.ID_SAVE,'Save','保存當前設置')
        filemenu.AppendSeparator()
        menuexit = filemenu.Append(wx.ID_EXIT,'Exit','退出程序')
        menuBar.Append(filemenu,'File')

        menuabout = aboutmenu.Append(wx.ID_ABOUT,'Info','Information')
        menuBar.Append(aboutmenu,'Info')

        #將menu與函數綁定
        self.Bind(wx.EVT_MENU,self.Exit,menuexit)
        self.Bind(wx.EVT_MENU,self.Info,menuabout)
        self.Bind(wx.EVT_MENU,self.Open,menuopen)
        self.Bind(wx.EVT_MENU,self.Save,menusave)

        self.SetMenuBar(menuBar)

    def Exit(self,event):
            print 'aaaa'
            self.Close()

    def Open(self,event):
        self.dirname=''
        self.filename=''
        dlg = wx.FileDialog(self,'選擇文件',self.dirname,'','*.csv*',wx.FD_OPEN)
        if dlg.ShowModal() == wx.ID_OK:
            self.filename = dlg.GetFilename()
            self.dirname = dlg.GetDirectory()
            self.FilePath = os.path.join(self.dirname,self.filename)
            return self.FilePath

    def Save(self):
        #可以將要保存的東西放入本地磁盤
        pass

    def Info(self,event):
        self.messageinfo='Author:testuser\nDate:2019/5/21\nVersion:0.1'
        message = wx.MessageDialog(self,self.messageinfo,'INFO',wx.OK)
        message.ShowModal()
        message.Destroy()

    def InitStatusBar(self):
        #創建狀態欄
        statusbar = self.CreateStatusBar()
        #將狀態欄分割為3個部分
        statusbar.SetFieldsCount(3)
        #分割狀態欄的比例為3:2:1,用負數表示
        statusbar.SetStatusWidths([-3,-2,-1])
        #每部分狀態欄顯示的值,當鼠標停在menu上時,0號狀態欄會臨時顯示上面menu里的提示信息
        statusbar.SetStatusText('1111',0)
        statusbar.SetStatusText('2222',1)
        statusbar.SetStatusText('3333',2)


if __name__ == '__main__':
    app = MyApp()
    frame = MyFrame(title='wx-test',size=(400,300))
    frame.Show()
    app.MainLoop()

運行結果:

 

 

2.將button直接放在Frame上

        #self.panel=wx.Panel(self)
        #button=wx.Button(self.panel,label=u'Start',size=(50,30),pos=(0,30))
        button=wx.Button(self,label=u'開始',size=(50,30),pos=(0,30)) #將button直接放在Frame上

運行結果:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM