Python/Programação com GUI

De Wikibooks

Voltar para a página inicial.


Tabela de conteúdo

[editar] Tkinter

Baseada em Tcl/Tk, o Tkinter é a interface padrão do Python.

from Tkinter import *
 
class App:
    def __init__(self, master):
        frame = Frame(master)
        frame.pack()
        self.button = Button(frame, text="QUIT", fg="red",
                         command=frame.quit)
        self.button.pack(side=LEFT)
        self.hi_there = Button(frame, text="Hello",
                           command=self.say_hi)
        self.hi_there.pack(side=LEFT)
 
    def say_hi(self):
        print "hi there, everyone!"
 
root = Tk()
app = App(root)
root.mainloop()

[editar] WxPython

wxPython é um wrapper da biblioteca wxWindows.

from wxPython.wx import *
 
ID_ABOUT = 101
ID_EXIT  = 102
 
class MyFrame(wxFrame):
    def __init__(self, parent, ID, title):
        wxFrame.__init__(self, parent, ID, title,
                         wxDefaultPosition, wxSize(200, 150))
        self.CreateStatusBar()
        self.SetStatusText("This is the statusbar")
        menu = wxMenu()
        menu.Append(ID_ABOUT, "&About", "More information about this program")
        menu.AppendSeparator()
        menu.Append(ID_EXIT, "E&xit", "Terminate the program")
        menuBar = wxMenuBar()
        menuBar.Append(menu, "&File");
        self.SetMenuBar(menuBar)
 
class MyApp(wxApp):
    def OnInit(self):
        frame = MyFrame(NULL, -1, "Hello from wxPython")
        frame.Show(true)
        self.SetTopWindow(frame)
        return true
 
app = MyApp(0)
app.MainLoop()

[editar] PyGTK

PyGTK é um wrapper para o Python criado por James Henstridge, construído sobre o GIMP Toolkit (GTK) a biblioteca usada pelo GNOME e muitas outras aplicações para Linux.

import gtk
 
class HelloWorld(gtk.Window):
    def __init__(self):
        gtk.Window.__init__(self)
        self.connect("delete_event", gtk.main_quit)
        self.set_border_width(10)
        self.set_title("Hello World!")
        hbox = gtk.HBox()
        self.add(hbox)
        self.button1 = gtk.Button("Button 1")
        self.button1.connect("clicked", self.button_pressed_cb)
        hbox.pack_start(self.button1)
        self.button2 = gtk.Button("Button 2")
        self.button2.connect("clicked", self.button_pressed_cb)
        hbox.pack_start(self.button2)
 
    def button_pressed_cb(self, button):
        print "Hello again - %s was pressed" % button.get_label()
 
if __name__ == "__main__":
    win = HelloWorld()
    win.show_all()
    gtk.main()

[editar] PyQt

PyQT é um wrapper da linguagem Python para a biblioteca Qt, que é a base do KDE (ambiente desktop para sistemas operacionais parecidos com unix), como o Linux, por exemplo.

import sys
from qt import *
 
class HelloButton(QPushButton):
    def __init__(self, *args):
        apply(QPushButton.__init__, (self,) + args)
        self.setText("Hello World")
 
class HelloWindow(QMainWindow):
    def __init__(self, *args):
        apply(QMainWindow.__init__, (self,) + args)
        self.button=HelloButton(self)
        self.setCentralWidget(self.button)
 
def main(args):
    app=QApplication(args)
    win=HelloWindow()
    win.show()
    app.connect(app, SIGNAL("lastWindowClosed()"), app, SLOT("quit()"))
    app.exec_loop()
 
if __name__=="__main__":
    main(sys.argv)

[editar] Pygame

Pygame é uma biblioteca multiplataforma construída sobre a SDL permitindo a criação de jogos e programas multimídia.

import sys, pygame
 
pygame.init()
 
tam = width, height = 320, 240
vel = [2, 2]
pet = 0, 0, 0
 
tela = pygame.display.set_mode(tam)
bola = pygame.image.load("image.png")
bolarc = bola.get_rect()
 
tela.fill(pet)
tela.blit(bola, bolarc)
pygame.display.flip()
Ferramentas pessoais