<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                本節看一下文件對話框的使用,先看實例,再介紹具體知識點。 代碼: ~~~ #!/usr/bin/env python # -*- coding: utf-8 -*- ''' Function:繪圖 Input:NONE Output: NONE author: socrates blog:http://www.cnblogs.com/dyx1024/ date:2012-07-14 ''' import wx import cPickle import os class PaintWindow(wx.Window): def __init__(self, parent, id): wx.Window.__init__(self, parent, id) self.SetBackgroundColour("Red") self.color = "Green" self.thickness = 10 #創建一個畫筆 self.pen = wx.Pen(self.color, self.thickness, wx.SOLID) self.lines = [] self.curLine = [] self.pos = (0, 0) self.InitBuffer() #連接事件 self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown) self.Bind(wx.EVT_LEFT_UP, self.OnLeftUp) self.Bind(wx.EVT_MOTION, self.OnMotion) self.Bind(wx.EVT_SIZE, self.OnSize) self.Bind(wx.EVT_IDLE, self.OnIdle) self.Bind(wx.EVT_PAINT, self.OnPaint) def InitBuffer(self): size = self.GetClientSize() #創建緩存的設備上下文 self.buffer = wx.EmptyBitmap(size.width, size.height) dc = wx.BufferedDC(None, self.buffer) #使用設備上下文 dc.SetBackground(wx.Brush(self.GetBackgroundColour())) dc.Clear() self.DrawLines(dc) self.reInitBuffer = False def GetLinesData(self): return self.lines[:] def SetLinesData(self, lines): self.lines = lines[:] self.InitBuffer() self.Refresh() def OnLeftDown(self, event): self.curLine = [] #獲取鼠標位置 self.pos = event.GetPositionTuple() self.CaptureMouse() def OnLeftUp(self, event): if self.HasCapture(): self.lines.append((self.color, self.thickness, self.curLine)) self.curLine = [] self.ReleaseMouse() def OnMotion(self, event): if event.Dragging() and event.LeftIsDown(): dc = wx.BufferedDC(wx.ClientDC(self), self.buffer) self.drawMotion(dc, event) event.Skip() def drawMotion(self, dc, event): dc.SetPen(self.pen) newPos = event.GetPositionTuple() coords = self.pos + newPos self.curLine.append(coords) dc.DrawLine(*coords) self.pos = newPos def OnSize(self, event): self.reInitBuffer = True def OnIdle(self, event): if self.reInitBuffer: self.InitBuffer() self.Refresh(False) def OnPaint(self, event): dc = wx.BufferedPaintDC(self, self.buffer) def DrawLines(self, dc): for colour, thickness, line in self.lines: pen = wx.Pen(colour, thickness, wx.SOLID) dc.SetPen(pen) for coords in line: dc.DrawLine(*coords) def SetColor(self, color): self.color = color self.pen = wx.Pen(self.color, self.thickness, wx.SOLID) def SetThickness(self, num): self.thickness = num self.pen = wx.Pen(self.color, self.thickness, wx.SOLID) class PaintFrame(wx.Frame): def __init__(self, parent): self.title = "Paint Frame" wx.Frame.__init__(self, parent, -1, self.title, size = (800, 600)) self.paint = PaintWindow(self, -1) #狀態欄 self.paint.Bind(wx.EVT_MOTION, self.OnPaintMotion) self.InitStatusBar() #創建菜單 self.CreateMenuBar() self.filename = "" def InitStatusBar(self): self.statusbar = self.CreateStatusBar() #將狀態欄分割為3個區域,比例為1:2:3 self.statusbar.SetFieldsCount(3) self.statusbar.SetStatusWidths([-1, -2, -3]) def OnPaintMotion(self, event): #設置狀態欄1內容 self.statusbar.SetStatusText(u"鼠標位置:" + str(event.GetPositionTuple()), 0) #設置狀態欄2內容 self.statusbar.SetStatusText(u"當前線條長度:%s" % len(self.paint.curLine), 1) #設置狀態欄3內容 self.statusbar.SetStatusText(u"線條數目:%s" % len(self.paint.lines), 2) event.Skip() def MenuData(self): ''' 菜單數據 ''' #格式:菜單數據的格式現在是(標簽, (項目)),其中:項目組成為:標簽, 描術文字, 處理器, 可選的kind #標簽長度為2,項目的長度是3或4 return [("&File", ( #一級菜單項 ("&New", "New paint file", self.OnNew), #二級菜單項 ("&Open", "Open paint file", self.OnOpen), ("&Save", "Save paint file", self.OnSave), ("", "", ""), #分隔線 ("&Color", ( ("&Black", "", self.OnColor, wx.ITEM_RADIO), #三級菜單項,單選 ("&Red", "", self.OnColor, wx.ITEM_RADIO), ("&Green", "", self.OnColor, wx.ITEM_RADIO), ("&Blue", "", self.OnColor, wx.ITEM_RADIO))), ("", "", ""), ("&Quit", "Quit", self.OnCloseWindow))) ] def CreateMenuBar(self): ''' 創建菜單 ''' menuBar = wx.MenuBar() for eachMenuData in self.MenuData(): menuLabel = eachMenuData[0] menuItems = eachMenuData[1] menuBar.Append(self.CreateMenu(menuItems), menuLabel) self.SetMenuBar(menuBar) def CreateMenu(self, menuData): ''' 創建一級菜單 ''' menu = wx.Menu() for eachItem in menuData: if len(eachItem) == 2: label = eachItem[0] subMenu = self.CreateMenu(eachItem[1]) menu.AppendMenu(wx.NewId(), label, subMenu) #遞歸創建菜單項 else: self.CreateMenuItem(menu, *eachItem) return menu def CreateMenuItem(self, menu, label, status, handler, kind = wx.ITEM_NORMAL): ''' 創建菜單項內容 ''' if not label: menu.AppendSeparator() return menuItem = menu.Append(-1, label, status, kind) self.Bind(wx.EVT_MENU, handler,menuItem) def OnNew(self, event): pass def OnOpen(self, event): ''' 打開開文件對話框 ''' file_wildcard = "Paint files(*.paint)|*.paint|All files(*.*)|*.*" dlg = wx.FileDialog(self, "Open paint file...", os.getcwd(), style = wx.OPEN, wildcard = file_wildcard) if dlg.ShowModal() == wx.ID_OK: self.filename = dlg.GetPath() self.ReadFile() self.SetTitle(self.title + '--' + self.filename) dlg.Destroy() def OnSave(self, event): ''' 保存文件 ''' if not self.filename: self.OnSaveAs(event) else: self.SaveFile() def OnSaveAs(self, event): ''' 彈出文件保存對話框 ''' file_wildcard = "Paint files(*.paint)|*.paint|All files(*.*)|*.*" dlg = wx.FileDialog(self, "Save paint as ...", os.getcwd(), style = wx.SAVE | wx.OVERWRITE_PROMPT, wildcard = file_wildcard) if dlg.ShowModal() == wx.ID_OK: filename = dlg.GetPath() if not os.path.splitext(filename)[1]: #如果沒有文件名后綴 filename = filename + '.paint' self.filename = filename self.SaveFile() self.SetTitle(self.title + '--' + self.filename) dlg.Destroy() def OnColor(self, event): ''' 更改畫筆內容 ''' menubar = self.GetMenuBar() itemid = event.GetId() item = menubar.FindItemById(itemid) color = item.GetLabel() #獲取菜單項內容 self.paint.SetColor(color) def OnCloseWindow(self, event): self.Destroy() def SaveFile(self): ''' 保存文件 ''' if self.filename: data = self.paint.GetLinesData() f = open(self.filename, 'w') cPickle.dump(data, f) f.close() def ReadFile(self): if self.filename: try: f = open(self.filename, 'r') data = cPickle.load(f) f.close() self.paint.SetLinesData(data) except cPickle.UnpicklingError: wx.MessageBox("%s is not a paint file." % self.filename, "error tip", style = wx.OK | wx.ICON_EXCLAMATION) if __name__ == '__main__': app = wx.PySimpleApp() frame = PaintFrame(None) frame.Show(True) app.MainLoop() ~~~ 測試: 1、保存對話框: ![](https://box.kancloud.cn/2016-06-08_575793608ac0c.png) 2、打開對話框: ![](https://box.kancloud.cn/2016-06-08_575793609d852.png) 知識點介紹: wx.FileDialog原型: wxFileDialog([wxWindow](http://www.cnblogs.com/dyx1024/admin/)*?parent,?const?[wxString](http://www.cnblogs.com/dyx1024/admin/)&?message = "Choose a file",?const?[wxString](http://www.cnblogs.com/dyx1024/admin/)&?defaultDir = "",?const?[wxString](http://www.cnblogs.com/dyx1024/admin/)&defaultFile = "",?const?[wxString](http://www.cnblogs.com/dyx1024/admin/)&?wildcard = "*.*",?long?style = wxFD_DEFAULT_STYLE,?const?[wxPoint](http://www.cnblogs.com/dyx1024/admin/)&?pos = wxDefaultPosition,const?[wxSize](http://www.cnblogs.com/dyx1024/admin/)&?sz = wxDefaultSize,?const?[wxString](http://www.cnblogs.com/dyx1024/admin/)&?name = "filedlg") 方法: - wxFileDialog::wxFileDialog - wxFileDialog::~wxFileDialog - wxFileDialog::GetDirectory - wxFileDialog::GetFilename - wxFileDialog::GetFilenames - wxFileDialog::GetFilterIndex - wxFileDialog::GetMessage - wxFileDialog::GetPath - wxFileDialog::GetPaths - wxFileDialog::GetWildcard - wxFileDialog::SetDirectory - wxFileDialog::SetFilename - wxFileDialog::SetFilterIndex - wxFileDialog::SetMessage - wxFileDialog::SetPath - wxFileDialog::SetWildcard - wxFileDialog::ShowModal 參數: - parentParent window. - messageMessage to show on the dialog. - defaultDirThe default directory, or the empty string. - defaultFileThe default filename, or the empty string. - wildcard A wildcard, such as "*.*" or "BMP files (*.bmp)|*.bmp|GIF files (*.gif)|*.gif". ? ? ? ? ? ? ? Note that the native Motif dialog has some limitations with respect to wildcards; see the Remarks section above. - styleA dialog style. See wxFD_* styles for more info. - posDialog position. Not implemented. - sizeDialog size. Not implemented. - name Dialog name. Not implemented. ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 附:Style取值 | wxFD_DEFAULT_STYLE | Equivalent to wxFD_OPEN. | |-----|-----| | wxFD_OPEN | This is an open dialog; usually this means that the default button's label of the dialog is "Open". Cannot be combined with wxFD_SAVE. | | wxFD_SAVE | This is a save dialog; usually this means that the default button's label of the dialog is "Save". Cannot be combined with wxFD_OPEN. | | wxFD_OVERWRITE_PROMPT | For save dialog only: prompt for a confirmation if a file will be overwritten. | | wxFD_FILE_MUST_EXIST | For open dialog only: the user may only select files that actually exist. | | wxFD_MULTIPLE | For open dialog only: allows selecting multiple files. | | wxFD_CHANGE_DIR | Change the current working directory to the directory where the file(s) chosen by the user are. | | wxFD_PREVIEW | Show the preview of the selected files (currently only supported by wxGTK using GTK+ 2.4 or later).? |
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看