您的位置:首页 > 编程语言 > Python开发

python学习第二天

2015-08-16 22:42 856 查看
今天用wxpython 实现简单的menu、 button 并为其绑定点击事件和对话框输出,很简单, 但还是需要好好

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#  -*- coding: utf-8 -*-才能支持中文

import os
import wx

class MainWindow(wx.Frame):
	def __init__(self, parent, title):
		wx.Frame.__init__(self, parent, title=title,size=(500,500))
		# self.control = wx.TextCtrl(self, style = wx.TE_MULTILINE)
		panel = wx.Panel(self)
		self.setupMenuBar()
		self.setupButton(panel)
		self.Show(True)

	def setupButton(self, panel):
		button = wx.Button(panel, label = u"关闭", pos = (150, 60), size = (100, 60))
		self.Bind(wx.EVT_BUTTON, self.onAbout, button)
         
	def setupMenuBar(self):
		self.CreateStatusBar()
		menubar = wx.MenuBar()
		menufile = wx.Menu()

		menuabout = menufile.Append(wx.ID_ABORT, '&About', 'about this shit')
		menuexit = menufile.Append(wx.ID_EXIT, '&Exit', 'end program')
		menubar.Append(menufile, '&File')
 
		self.Bind(wx.EVT_MENU, self.onAbout, menuabout)
		self.Bind(wx.EVT_MENU, self.onExit, menuexit)
		self.SetMenuBar(menubar)

	def onAbout(self, evt):
		dlg = wx.MessageDialog(self, u'确实要关闭吗?', u'关闭', wx.YES_NO | wx.ICON_QUESTION)
		if wx.ID_YES == dlg.ShowModal():
			self.Close(True)

		dlg.Destroy()

	def onExit(self, evt):
		self.Close(True)

def main():
	app = wx.App(False)
	frame = MainWindow(None, 'Small Editor')
	app.MainLoop()
if __name__ == '__main__':
	main()


实现效果如图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: