1.Tkinter label(设置背景图片jpg)
运行效果:
============================================
代码部分:
============================================
from Tkinter import * from PIL import Image,ImageTk def main(): filename = Image.open('D:\\pop.jpg') root = Tk() img = ImageTk.PhotoImage(filename) label = Label(root, image=img) label.pack() root.mainloop() main()
2.Tkinter canvas(设置背景图片及文字)
运行效果:
============================================
代码部分:
============================================
from Tkinter import * from PIL import Image,ImageTk root = Tk() canvas = Canvas(root,width=500,height=600, bg='white') image=Image.open("pop.jpg") im = ImageTk.PhotoImage(image) canvas.create_image(300,80,image=im) canvas.create_text(302,77,text='Use Canvas',fill='blue') canvas.pack() root.mainloop()
3.wxpython消息对话框
运行效果:
============================================
代码部分:
============================================
import wx class Frame(wx.Frame): pass class App(wx.App): def OnInit(self): frame = Frame(parent=None,title='Spare') frame.Show() dlg = wx.MessageDialog(None, 'Is this the coolest thing ever!', 'MessageDialog', wx.YES_NO | wx.ICON_QUESTION) result = dlg.ShowModal() dlg.Destroy() self.SetTopWindow(frame) return True if __name__ == '__main__': app = App() app.MainLoop()
4.wxpython 创建一个简单的窗口
运行效果:
============================================
代码部分:
============================================
#!/usr/bin/env python import wx from wx.py.shell import ShellFrame from wx.py.filling import FillingFrame import wx.py.images as images class ToolbarFrame(wx.Frame): def __init__(self, parent, id): wx.Frame.__init__(self, parent, id, 'Toolbars', size=(300, 200)) panel = wx.Panel(self) panel.SetBackgroundColour('White') statusBar = self.CreateStatusBar() #1 创建状态栏 toolbar = self.CreateToolBar() #2 创建工具栏 toolbar.AddSimpleTool(wx.NewId(), images.getPyBitmap(), "New", "Long help for 'New'") #3 给工具栏增加一个工具 toolbar.Realize() #4 准备显示工具栏 menuBar = wx.MenuBar() # 创建菜单栏 # 创建两个菜单 menu1 = wx.Menu() menuBar.Append(menu1, " ") menu2 = wx.Menu() #6 创建菜单的项目 menu2.Append(wx.NewId(), " ", "Copy in status bar") menu2.Append(wx.NewId(), "C ", "") menu2.Append(wx.NewId(), "Paste", "") menu2.AppendSeparator() menu2.Append(wx.NewId(), " ", "Display Options") menuBar.Append(menu2, " ") # 在菜单栏上附上菜单 menu3 = wx.Menu() shell = menu3.Append(-1," shell","open wxpython shell frame") filling = menu3.Append(-1," viewer","open namespace viewer frame") menuBar.Append(menu3, " ") self.Bind(wx.EVT_MENU,self.OnShell,shell) self.Bind(wx.EVT_MENU,self.OnFilling,filling) self.SetMenuBar(menuBar) # 在框架上附上菜单栏 def OnCloseMe(self,event): self.Close(True) def OnCloseWindow(self,event): self.Destory() def OnShell(self,event): frame = ShellFrame(parent=self) frame.Show() def OnFilling(self,event): frame = FillingFrame(parent=self) frame.Show() if __name__ == '__main__': app = wx.PySimpleApp() frame = ToolbarFrame(parent=None, id=-1) frame.Show() app.MainLoop()
5.使用wx.html.HtmlWindow作为一个about框
运行效果:
============================================
代码部分:
============================================
import wx import wx.html class SketchAbout(wx.Dialog): text = ''' <html> <body bgcolor="#ACAA60"> <center><table bgcolor="#455481" width="100%" cellspacing="0" cellpadding="0" border="1"> <tr> <td align="center"><h1>Sketch!</h1></td> </tr> </table> </center> <p><b>Sketch</b> is a demonstration program for <b>wxPython In Action</b> Chapter 6. It is based on the SuperDoodle demo included with wxPython, available at http://www.wxpython.org/ </p> <p><b>SuperDoodle</b> and <b>wxPython</b> are brought to you by <b>Robin Dunn</b> and <b>Total Control Software</b>, Copyright © 1997-2006.</p> </body> </html>s ''' def __init__(self, parent): wx.Dialog.__init__(self, parent, -1, 'About Sketch', size=(440, 400) ) html = wx.html.HtmlWindow(self) html.SetPage(self.text) button = wx.Button(self, wx.ID_OK, "Okay") sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add(html, 1, wx.EXPAND|wx.ALL, 5) sizer.Add(button, 0, wx.ALIGN_CENTER|wx.ALL, 5) self.SetSizer(sizer) self.Layout() if __name__ == '__main__': app = wx.PySimpleApp() about = SketchAbout(None) about.Show(True) app.MainLoop()
6.显示启动画面
============================================
代码部分:
============================================
#-*- coding: utf-8 -*- import wx from a1 import SketchFrame class SketchApp(wx.App): def OnInit(self): bmp = wx.Image("pop.jpg").ConvertToBitmap() wx.SplashScreen(bmp, wx.SPLASH_CENTRE_ON_SCREEN | wx.SPLASH_TIMEOUT, 10000, None, -1) wx.Yield() frame = SketchFrame(None) frame.Show(True) self.SetTopWindow(frame) return True if __name__ == '__main__': app = SketchApp() app.MainLoop()
7.创建个性菜单
运行效果:
============================================
代码部分:
============================================
import wx class MyFrame(wx.Frame): def __init__(self): wx.Frame.__init__(self, None, -1, "Fancier Menu Example") p = wx.Panel(self) menu = wx.Menu() bmp = wx.Bitmap("pop1.png", wx.BITMAP_TYPE_PNG) item = wx.MenuItem(menu, -1, "Has Open Bitmap") item.SetBitmap(bmp)#增加一个自定义的位图 menu.AppendItem(item) if True or 'wxMSW' in wx.PlatformInfo: font = wx.SystemSettings.GetFont( wx.SYS_DEFAULT_GUI_FONT) font.SetWeight(wx.BOLD) item = wx.MenuItem(menu, -1, "Has Bold Font") item.SetFont(font)#改变字体 menu.AppendItem(item) item = wx.MenuItem(menu, -1, "Has Red Text") item.SetTextColour("red")#改变文本颜色 menu.AppendItem(item) menu.AppendSeparator() exit = menu.Append(-1, "Exit") self.Bind(wx.EVT_MENU, self.OnExit, exit) menuBar = wx.MenuBar() menuBar.Append(menu, "Menu") self.SetMenuBar(menuBar) def OnExit(self, event): self.Close() if __name__ == "__main__": app = wx.PySimpleApp() frame = MyFrame() frame.Show() app.MainLoop()
8.BoxSizer
运行效果:
============================================
代码部分:
============================================
#coding=utf-8 import wx class MyFrame(wx.Frame): def __init__(self): wx.Frame.__init__(self, None, -1, "Simple Menu Example") p = wx.Panel(self) menuBar = wx.MenuBar()# 创建一个菜单栏 menu = wx.Menu()# 创建一个菜单 menuBar.Append(menu, "Left Menu")# 添加菜单到菜单栏 menu2 = wx.Menu() menuBar.Append(menu2, "Middle Menu") menu3 = wx.Menu() menuBar.Append(menu3, "Right Menu") self.SetMenuBar(menuBar) sizer = wx.BoxSizer(wx.HORIZONTAL) #sizer.Add(self.html,1, wx.ALL|wx.EXPAND,0) p.SetSizer(sizer) sizer.Fit(self) if __name__ == "__main__": app = wx.PySimpleApp() frame = MyFrame() frame.Show() app.MainLoop()
9.公司网站
# -*- coding: gbk -*- import wx,sys import wx.lib.iewin as iewin import wx.html import time from comtypes.gen import MSHTML import wx.py.images as images from win32com.client import Dispatch from wx.lib.wordwrap import wordwrap APP = 1 APP_BAIDU = 2 #创建自己的ID ID_MENU_EX = wx.NewId() ID_MENU_EXCHANGE = wx.NewId() ID_MENU_AMS = wx.NewId() #self.Bind(wx.EVT_MENU, self.DisplayMessage,id=ID_MENU_NEW) #启动画面 class SketchApp(wx.App): def OnInit(self): bmp = wx.Image("D:\mp3\mvpc.png").ConvertToBitmap() wx.SplashScreen(bmp, wx.SPLASH_CENTRE_ON_PARENT|wx.SPLASH_NO_CENTRE|wx.SPLASH_TIMEOUT, 10000, None, -1) wx.Yield() frame = myForm() #frame.Show() #self.SetTopWindow(frame) return True #使用wx.html.HtmlWindow作为一个about框 class SketchAbout(wx.Dialog): text = ''' <html> <body bgcolor="#ACAA60"> <center><table bgcolor="#455481" width="100%" cellspacing="0" cellpadding="0" border="1"> <tr> <td align="center"><h1>Sketch!</h1></td> </tr> </table> </center> <p><b>Sketch</b> is a demonstration program for <b>wxPython In Action</b> Chapter 6. It is based on the SuperDoodle demo included with wxPython, available at http://www.wxpython.org/ </p> <p><b>SuperDoodle</b> and <b>wxPython</b> are brought to you by <b>Robin Dunn</b> and <b>Total Control Software</b>, Copyright © 1997-2006.</p> </body> </html>s ''' def __init__(self, parent): wx.Dialog.__init__(self, parent, -1, 'About Sketch', size=(440, 400) ) html = wx.html.HtmlWindow(self) html.SetPage(self.text) button = wx.Button(self, wx.ID_OK, "Okay") sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add(html, 1, wx.EXPAND|wx.ALL, 5) sizer.Add(button, 0, wx.ALIGN_CENTER|wx.ALL, 5) self.SetSizer(sizer) self.Layout() class popupmenu(wx.Menu): def __init__(self,parent): super(popupmenu,self).__init__() self.parent = parent mmi = wx.MenuItem(self,wx.NewId(),'MiniSize') self.AppendItem(mmi) self.Bind(wx.EVT_MENU,self.OnMinimize,mmi) cmi = wx.MenuItem(self,wx.NewId(),'Close') self.AppendItem(cmi) self.Bind(wx.EVT_MENU,self.OnClose,cmi) def OnMinimize(self,e): self.parent.Iconize() def OnClose(self,e): self.parent.Close() class myForm: url1 = 'http://www.1exchange.cc' url2 = 'http://www.1ex888.com' url3 = 'www.sportslotto.mn' url4 = 'mail.dh5678.com' url5 = 'http://www.18x.co:8188' url6 = 'http://www.1ex888.cc:81/tms' url7 = 'http://ams.1ex888.cc:8181' url8 = 'http://www.baidu.com/' a = 0 def __init__(self): #wx.Frame.__init__(self,None,title='eggDragon',size=(800,600)) self.frame = wx.Frame(parent=None,title='Dragon',size=(800,600)) #p = wx.Panel(self) #按钮和用户输入 #self.txt = wx.TextCtrl(self.frame, -1, "new web site",pos=(40,10)) self.txt = wx.TextCtrl(self.frame, -1, "new web site") self.btn = wx.Button(self.frame, -1, "Add") self.frame.Bind(wx.EVT_BUTTON, self.OnAddItem, self.btn)# 绑定按钮的事件 #url菜单和事件 self.menu = menu = wx.Menu() menu2 =wx.Menu() menu3 =wx.Menu() menu5 =wx.Menu() imp =wx.Menu() menuBar = wx.MenuBar() menuBar.Append(menu, "&ZTime") menuBar.Append(menu2, "防御点") menuBar.Append(menu3, "域名管理") exchange = menu.Append(ID_MENU_EXCHANGE, "exchange") menu.AppendSeparator() ex = menu.Append(ID_MENU_EX,"ex") self.frame.Bind(wx.EVT_MENU, self.DisplayMessage, id=ID_MENU_EXCHANGE) self.frame.Bind(wx.EVT_MENU, self.DisplayMessage, id=ID_MENU_EX) defence_ex = menu2.Append(-1, "ex防御点") menu.AppendSeparator() defence_tms = menu2.Append(-1,"tms防御点") defence_ams = menu2.Append(-1,"ams防御点") self.frame.Bind(wx.EVT_MENU, self.defence_ex, defence_ex) self.frame.Bind(wx.EVT_MENU, self.defence_tms, defence_tms) self.frame.Bind(wx.EVT_MENU, self.OnAbout, defence_ams) #ie broswer self.ie = iewin.IEHtmlWindow(parent=self.frame) self.ie.AddEventSink(self) #sizer vbox = wx.BoxSizer(wx.VERTICAL) sizer = wx.BoxSizer(wx.HORIZONTAL) #self.frame.SetSizer(sizer) #box.Add(self.userList,1,wx.ALIGN_LEFT|wx.EXPAND,10) #sizer.Add(self.ie,4,wx.ALIGN_RIGHT|wx.EXPAND,20) sizer.Add(self.btn) sizer.Add(self.txt,flag=wx.EXPAND,proportion=1) vbox.Add(sizer,flag=wx.EXPAND,border=10) #vbox.Add((-1,10)) hbox3 = wx.BoxSizer(wx.HORIZONTAL) hbox3.Add(self.ie, 1,wx.EXPAND,20) vbox.Add(hbox3, proportion=1, flag=wx.EXPAND, border=10) self.frame.SetSizer(vbox) # create "Play" menuitem menuplay =wx.Menu() menuBar.Append(menuplay, "&play(&P)") pause = menuplay.Append(-1,"Pause","Play the music file") Next=menuplay.Append(-1,"Next","Play Next song") stop=menuplay.Append(-1,"Stop","stop") menuplay.AppendSeparator() #改变字体和色彩 if True or 'wxMSW' in wx.PlatformInfo: font = wx.SystemSettings.GetFont( wx.SYS_DEFAULT_GUI_FONT) font.SetWeight(wx.BOLD) item1 = wx.MenuItem(menu, -1, "ex") item1.SetFont(font)#改变字体 menu.AppendItem(item1) item2 = wx.MenuItem(menu, -1, "ex") item2.SetTextColour("red")#改变文本颜色 menu.AppendItem(item2) self.frame.Bind(wx.EVT_MENU, self.ex,item1) self.frame.Bind(wx.EVT_MENU, self.ex,item2) #快捷键 qmi = wx.MenuItem(menu,APP,"&ZTime\tF2") qmi.SetBitmap(wx.Bitmap("D:\m.png")) menu.AppendItem(qmi) self.frame.Bind(wx.EVT_MENU, self.defence_ams, id=APP) bd = wx.MenuItem(menu,APP_BAIDU,"&Baidu\tF1") bd.SetBitmap(wx.Bitmap("D:\mp3\pop.jpg")) menu.AppendItem(bd) self.frame.Bind(wx.EVT_MENU, self.baidu, id=APP_BAIDU) #子菜单 imp.Append(wx.ID_ANY,'Import newsfeed list...') imp.Append(wx.ID_ANY,'Import bookmarks...') imp.Append(wx.ID_ANY,'Import mail...') menu.AppendMenu(wx.ID_ANY,'I&mport',imp) #创建工具栏和状态栏 self.statusbar = self.frame.CreateStatusBar() self.toolBar = self.frame.CreateToolBar() toolBar_open = self.toolBar.AddSimpleTool(-1,wx.Bitmap('D:\mp3\meitu.png'),"open","Open a new file") toolBar_play = self.toolBar.AddSimpleTool(-1,wx.Bitmap('D:\mp3\play.png'),"play","Start to play") toolBar_next = self.toolBar.AddSimpleTool(-1,wx.Bitmap('D:\mp3\dragon1.png'),"Next","play next file") toolBar_stop = self.toolBar.AddSimpleTool(-1,wx.Bitmap('D:\mp3\pause.png'),"Stop","Stop the music") #self.toolBar.AddSimpleTool(-1,images.getPyBitmap(),"Tools","Tools") self.toolBar.Realize() #显示或隐藏工具栏和状态栏 menuBar.Append(menu5, '&View' ) self.st = menu5.Append(wx.ID_ANY,"ShowStatubar","ShowStatubar",kind=wx.ITEM_CHECK) self.tl = menu5.Append(wx.ID_ANY,"ShowToolBar","ShowToolBar",kind=wx.ITEM_CHECK) menu5.Check(self.st.GetId(),True) menu5.Check(self.tl.GetId(),True) self.frame.Bind(wx.EVT_MENU,self.ToggleStatuBar,self.st) self.frame.Bind(wx.EVT_MENU,self.ToggleToolBar,self.tl) self.frame.Bind(wx.EVT_RIGHT_DOWN,self.OnRightDown) #否决事件 self.frame.Bind(wx.EVT_CLOSE, self.OnCloseWindow) #键盘事件 self.frame.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown) self.frame.SetFocus() # mp3事件 self.frame.Bind(wx.EVT_TOOL,self.OnOpen,toolBar_open) self.frame.Bind(wx.EVT_TOOL,self.OnPlay,toolBar_play) self.frame.Bind(wx.EVT_TOOL,self.OnNext,toolBar_next) self.frame.Bind(wx.EVT_TOOL,self.OnStop,toolBar_stop) self.wmp = Dispatch('WMPlayer.OCX') #show self.frame.SetMenuBar(menuBar) self.frame.Show() def OnSimple(self, event): wx.MessageBox("You selected the simple menu item") print myFrom.a def OnExit(self, event): self.Close() def OnAddItem(self, event): item = self.menu.Append(-1, self.txt.GetValue())# 添加菜单项 self.frame.Bind(wx.EVT_MENU, self.OnNewItemSelected, item)# 绑定一个菜单事件 def OnNewItemSelected(self, event): #wx.MessageBox("You selected a new item") url = self.txt.GetValue() if url == self.url3: self.ie.LoadUrl(url) elif url == self.url4: self.ie.LoadUrl(url) else: wx.MessageBox("Please input the correct web site.") def DisplayMessage(self,e): stbar = self.frame.GetStatusBar() eid = e.GetId() if eid == ID_MENU_EXCHANGE: msg = '1exchange selected' stbar.SetStatusText(msg) self.ie.LoadUrl(self.url1) elif eid == ID_MENU_EX: msg = 'ex selected' stbar.SetStatusText(msg) self.ie.LoadUrl(self.url2) else: msg = '' stbar.SetStatusText(msg) def exchange(self,event): self.ie.LoadUrl(self.url1) def ex(self,event): self.ie.LoadUrl(self.url2) def defence_ex(self,event): self.ie.LoadUrl(self.url5) def defence_tms(self,event): self.ie.LoadUrl(self.url6) def defence_ams(self,event): self.ie.LoadUrl(self.url7) def baidu(self,event): self.ie.LoadUrl(self.url8) def ToggleStatuBar(self,event): if self.st.IsChecked(): self.statusbar.Show() else: self.statusbar.Hide() def ToggleToolBar(self,event): if self.tl.IsChecked(): self.toolBar.Show() else: self.toolBar.Hide() def OnRightDown(self,e): self.frame.PopupMenu(popupmenu(self.frame),e.GetPosition()) def OnCloseWindow(self,e): dial = wx.MessageDialog(None,"Are you Sure to Quit?","Question", wx.YES_NO|wx.NO_DEFAULT|wx.ICON_QUESTION) ret = dial.ShowModal() if ret == wx.ID_YES: self.frame.Destroy() else: e.Veto() def OnKeyDown(self,e): key = e.GetKeyCode() if key == wx.WXK_ESCAPE: ret = wx.MessageBox("Are you Sure to Quit?","Question", wx.YES_NO|wx.YES_DEFAULT) if ret == wx.YES: self.frame.Destroy() def dragon(self): myForm.a +=1 print myForm.a def OnOpen(self,event): dialog=wx.FileDialog(None,"open",style=wx.OPEN,wildcard="*.mp3") if dialog.ShowModal()==wx.ID_OK: self.filename=dialog.GetPath() if self.filename: media=self.wmp.newMedia(self.filename) self.wmp.currentPlaylist.appendItem(media) def OnPlay(self,event): self.wmp.controls.play() def OnPause(self,event): self.wmp.controls.pause() def OnNext(self,event): self.wmp.controls.next() def OnStop(self,event): self.wmp.controls.stop() def OnAbout(self, event): dlg = SketchAbout(None) dlg.ShowModal() dlg.Destroy() if __name__=='__main__': #app = wx.PySimpleApp() app = SketchApp() #app = wx.App(False) #form = myForm() #form.dragon() #form.Show() app.MainLoop()
10.wxpython 蛋蛋龙一键账户切换
运行效果:
============================================
代码部分:
============================================
import wx,sys import wx.lib.iewin as iewin import wx.html import time from comtypes.gen import MSHTML class myForm(): userlist = [] isLogin = False #logouturl = "http://www.renren.com/logout.do" eggurl = "http://apps.renren.com/dandanlong" homeurl = "http://www.renren.com/home" homesysurl = "http://www.renren.com/SysHome.do" def __init__(self): self.frame = wx.Frame(parent=None,title='eggDragon',size=(800,600)) #listbox self.userList = wx.ListBox(parent=self.frame) self.addUserInUserlist() self.userList.Bind(wx.EVT_LISTBOX,self.userLogin) #ie broswer self.ie = iewin.IEHtmlWindow(parent=self.frame) self.ie.AddEventSink(self) #sizer box = wx.BoxSizer(wx.HORIZONTAL) self.frame.SetSizer(box) box.Add(self.userList,1,wx.ALIGN_LEFT|wx.EXPAND,10) box.Add(self.ie,4,wx.ALIGN_RIGHT|wx.EXPAND,20) #show self.frame.Show() def addUserInUserlist(self): f = file('f.txt') for line in f.readlines(): user = line.split(' ')[0].strip() password = line.split(' ')[1].strip() self.userlist.append({'user':user,'pass':password}) self.userList.Append(user) def userLogin(self,evt): if self.isLogin: self.ie.LoadUrl(self.homesysurl) self.isLogin = False else: self.ie.LoadUrl(self.eggurl) def DocumentComplete(self,this,pDisp,URL): url = str(URL).split("'")[1] print url if url==self.eggurl and not self.isLogin: username = '' password = '' for user in self.userlist: if user['user']==self.userList.GetStringSelection(): username = user['user'] password = user['pass'] if username!='' and password!='': element = self.ie.ctrl.Document.getElementById("email") element3 = element.QueryInterface(MSHTML.IHTMLElement) element3.innerText = username element = self.ie.ctrl.Document.getElementById("password") element3 = element.QueryInterface(MSHTML.IHTMLElement) element3.innerText = password element = self.ie.ctrl.Document.getElementsByTagName("input") for e in element: if e.type=="submit": element3 = e.QueryInterface(MSHTML.IHTMLElement) element3.click() self.isLogin = True #if url==self.homesysurl: # self.ie.LoadUrl(self.eggurl) if url==self.homeurl: self.ie.LoadUrl(self.eggurl) if url==self.homesysurl: self.ie.LoadUrl(self.eggurl) if __name__=='__main__': app = wx.App(False) form = myForm() app.MainLoop()