[Python]核心编程之GUI编程(其他GUI初探)

来源:互联网 发布:设计app软件多少钱 编辑:程序博客网 时间:2024/06/16 04:22

一、Tk Interface eXtensions(Tix)


#! /usr/bin/env python#coding=utf-8from Tkinter import Label,Button,ENDfrom Tix import Tk,Control,ComboBoxtop = Tk()top.tk.eval('package require Tix')lb = Label(top,text='Animals (in pairs; min:pair,max:dozen)')lb.pack()ct = Control(top,label='Number:',integer=True,max=12,min=2,value=2,step=2)ct.label.config(font='Helvetica -12 bold')ct.pack()cb = ComboBox(top,label='Type',editable=True)for animal in ('dog','cat','hamster','python'):    cb.insert(END,animal)    cb.pack()qb = Button(top,text='Quit',command=top.quit,bg='red',fg='white')qb.pack()top.mainloop()


二、Python MegaWidgets(PMW)

#! /usr/bin/env python#coding=utf-8from Tkinter import Button,END,Label,Wfrom Pmw import initialise,ComboBox,Countertop = initialise()lb = Label(top,text='Animals (in pairs;min: pair,max: pair)')lb.pack()ct = Counter(top,labelpos=W,label_text='Number:',    datatype='integer',entryfield_value=2,    increment=2,entryfield_validate={'validator':    'integer','min':2,'max':12})ct.pack()cb = ComboBox(top,labelpos=W,label_text='Type:')for animal in ('dog','cat','panda','chicken'):    cb.insert(END,animal)cb.pack()top.mainloop()

三、wxWidgets和wxPython

#! /usr/bin/env python#coding=utf-8import wxclass MyFrame(wx.Frame):    def __init__(self,parent=None,id=-1,title=''):        wx.Frame.__init__(self,parent,id,title,size=(200,400))        top = wx.Panel(self)        sizer = wx.BoxSizer(wx.VERTICAL)        font = wx.Font(9,wx.SWISS,wx.NORMAL,wx.BOLD)        lb = wx.StaticText(top,-1,'Animals(in pairs: min:pair,max:pair)')        sizer.Add(lb)                c1 = wx.StaticText(top,-1,'Number:')        c1.SetFont(font)        ct = wx.SpinCtrl(top,-1,'2',min=2,max=12)        sizer.Add(c1)        sizer.Add(ct)                c2 = wx.StaticText(top,-1,'Type:')        c2.SetFont(font)                cb = wx.ComboBox(top,-1,'',choices=('dog','panda','cat','tiger'))        sizer.Add(c2)        sizer.Add(cb)                qb = wx.Button(top,-1,'quit')        qb.SetBackgroundColour('red')        qb.SetForegroundColour('white')        self.Bind(wx.EVT_BUTTON,lambda e:self.Close(True),qb)        sizer.Add(qb)                top.SetSizer(sizer)        self.Layout()        class MyApp(wx.App):    def OnInit(self):        frame = MyFrame(title='wxWidgets')        frame.Show()        self.SetTopWindow(frame)        return True    def main():    app = MyApp()    app.MainLoop()    if __name__ == '__main__':    main()    



四、GTK+和PyGTK

#! /usr/bin/env python#coding=utf-8import pygtkpygtk.require('2.0')import gtkimport pangoclass GTKapp(object):    def __init__(self):        top = gtk.Window(gtk.WINDOW_TOPLEVEL)        top.connect('delete_event',gtk.main_quit)        top.connect('destroy',gtk.main_quit)        box = gtk.VBox(False,0)        lb = gtk.Label('Animals (in pairs: min:pair,max:dozen)')        box.pack_start(lb)                sb = gtk.HBox(False,0)        adj = gtk.Adjustment(2,2,12,2,4,0)        sl = gtk.Label('Number:')        sl.modify_font(pango.FontDescription('Arial Bold 10'))        sb.pack_start(sl)        ct = gtk.SpinButton(adj,0,0)        sb.pack_start(ct)        box.pack_start(sb)                cb = gtk.HBox(False,0)        c2 = gtk.Label('Type:')        cb.pack_start(c2)        ce = gtk.combo_box_entry_new_text()        for animal in ('dog','panda','cat','tiger'):            ce.append_text(animal)        cb.pack_start(ce)        box.pack_start(cb)                qb = gtk.Button("")        red = gtk.gdk.color_parse('red')        sty = qb.get_style()        for st in (gtk.STATE_NORMAL,gtk.STATE_PRELIGHT,gtk.STATE_ACTIVE):            sty.bg[st] = red        qb.set_style(sty)        ql = qb.child        ql.set_markup('<span color="white">QUIT</span>')        qb.connect_object("clicked",gtk.Widget.destroy,top)        box.pack_start(qb)        top.add(box)        top.show_all()        if __name__ =='__main__':    animal = GTKapp()    gtk.main()                            


REF:Core Python Programming













0 0
原创粉丝点击