python_GUI

1. 需要安装wxPython软件

2.  GUI(图形用户界面)代码的编写顺序

  

  备注:

     1. 加入面板和布局管理器,可以使得组件的位置和大小更加灵活

3. 示例: 

#encoding=utf-8
import wx

#open按钮的事件处理函数
def load(event):
    try:
        myFile  = open(filename.GetValue())
        contents.SetValue(myFile.read())
        dlg = wx.MessageDialog(None, message = "Open Success!", style = wx.OK)
        if dlg.ShowModal() == wx.ID_OK:
            dlg.Close(True)
        dlg.Destroy()
        myFile.close()
    except IOError, e:
        dlg = wx.MessageDialog(None, message = "File not Found!", style = wx.OK)
        if dlg.ShowModal() == wx.ID_OK:
            dlg.Close(True)
        dlg.Destroy()
        
#save按钮的事件处理函数
def save(event):
    myFile = open(filename.GetValue(), "w")
    myFile.write(contents.GetValue())
    myFile.close()

    #保存成功后添加提示框
    dlg = wx.MessageDialog(None, message = "Save Success!", style = wx.OK)
    if dlg.ShowModal() == wx.ID_OK:
        dlg.Close(True)
        dlg.Destroy()



#打开GUI应用
app = wx.App()

#打开一个窗口
win = wx.Frame(None, title = "Simple Editor")

#窗口中加入面板
pl = wx.Panel(win)

#将组件放置在面板上
loadButton = wx.Button(pl, label = "Load")
saveButton = wx.Button(pl, label = "Save")
filename = wx.TextCtrl(pl)
contents = wx.TextCtrl(pl, style = wx.TE_MULTILINE | wx.HSCROLL)

#按钮绑定事件处理函数
loadButton.Bind(wx.EVT_BUTTON, load)
saveButton.Bind(wx.EVT_BUTTON, save)

#设置布局管理器(设置组件的大小和位置)
#水平布局管理器
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)

#垂直布局管理器
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.RIGHT | wx.BOTTOM, border = 5)

#设置面板的布局管理器
pl.SetSizer(vbox)

#显示窗口
win.Show()

#启动GUI应用
app.MainLoop()

  运行结果:

  

原文地址:https://www.cnblogs.com/zhuhaiying/p/5209830.html