python使用wxpython開發簡單記事本的方法

來源:互聯網
上載者:User

python使用wxpython開發簡單記事本的方法

   本文執行個體講述了python使用wxpython開發簡單記事本的方法。分享給大家供大家參考。具體分析如下:

  wxPython是Python程式設計語言的一個GUI工具箱。他使得Python程式員能夠輕鬆的建立具有健壯、功能強大的圖形化使用者介面的程式。它是Python語言對流行的wxWidgets跨平台GUI工具庫的綁定。而wxWidgets是用C++語言寫成的。

  和Python語言與wxWidgetsGUI工具庫一樣,wxPython是開源軟體。這意味著任何人都可以免費地使用它並且可以查看和修改它的原始碼,或者貢獻補丁,增加功能。

  wxPython是跨平台的。這意味著同一個程式可以不經修改地在多種平台上運行。現今支援的平台有:32位微軟Windows作業系統、大多數Unix或類Unix系統、蘋果MacOS X。

  下面使用wxpython編寫一個簡單的記事本程式,可以開啟本地檔案,編輯,儲存。

  ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

#!/usr/bin/python

import wx

def OnOpen(event):

"""

Load a file into the textField.

"""

dialog = wx.FileDialog(None,'Notepad',style = wx.OPEN)

if dialog.ShowModal() == wx.ID_OK:

filename.SetValue(dialog.GetPath())

file = open(dialog.GetPath())

contents.SetValue(file.read())

file.close()

dialog.Destroy()

def OnSave(event):

"""

Save text into the orignal file.

"""

if filename.GetValue() == '':

dialog = wx.FileDialog(None,'Notepad',style = wx.SAVE)

if dialog.ShowModal() == wx.ID_OK:

filename.SetValue(dialog.GetPath())

file = open(dialog.GetPath(), 'w')

file.write(contents.GetValue())

file.close()

dialog.Destory()

else:

file = open(filename.GetValue(), 'w')

file.write(contents.GetValue())

file.close()

app = wx.App()

win = wx.Frame(None, title="Simple Editor", size=(600,400))

bkg = wx.Panel(win)

# Define a 'load' button and its label,

# bind to an button event with a function 'load'

loadButton = wx.Button(bkg, label='Open')

loadButton.Bind(wx.EVT_BUTTON, OnOpen)

# Define a 'save' button and its label,

# bind to an button event with a function 'save'

saveButton = wx.Button(bkg, label='Save')

saveButton.Bind(wx.EVT_BUTTON, OnSave)

# Define a textBox for filename.

filename = wx.TextCtrl(bkg)

# Define a textBox for file contents.

contents = wx.TextCtrl(bkg, style=wx.TE_MULTILINE | wx.HSCROLL)

# Use sizer to set relative position of the components.

# Horizontal layout

hbox = wx.BoxSizer()

hbox.Add(filename, proportion=1, flag=wx.EXPAND)

hbox.Add(loadButton, proportion=0, flag=wx.LEFT, border=5)

hbox.Add(saveButton, proportion=0, flag=wx.LEFT, border=5)

# Vertical layout

vbox = wx.BoxSizer(wx.VERTICAL)

vbox.Add(hbox, proportion=0, flag=wx.EXPAND | wx.ALL, border=5)

vbox.Add(contents, proportion=1,

flag=wx.EXPAND | wx.LEFT | wx.BOTTOM | wx.RIGHT, border=5)

bkg.SetSizer(vbox)

win.Show()

app.MainLoop()

  運行效果如所示:

  這個例子是《Python基礎教程》中的一個例子,並做了一些修改。雖然完成了基本的記事本功能,但是介面略顯簡單,而且代碼也沒有很好地遵循物件導向編程原則。

  希望本文所述對大家的Python程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.