Python/Programação com GUI
Voltar para a página inicial.
Índice |
Tkinter[editar]
Ver artigo principal, Python/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()
WxPython[editar]
wxPython é um wrapper da biblioteca wxWindows.
from wx import * ID_ABOUT = 101 ID_EXIT = 102 class MyFrame(wx.Frame): def __init__(self, parent, ID, title): wx.Frame.__init__(self, parent, ID, title, wx.DefaultPosition, wx.Size(200, 150)) self.CreateStatusBar() self.SetStatusText("This is the statusbar") menu = wx.Menu() menu.Append(ID_ABOUT, "&About", "More information about this program") menu.AppendSeparator() menu.Append(ID_EXIT, "E&xit", "Terminate the program") menuBar = wx.MenuBar() menuBar.Append(menu, "&File"); self.SetMenuBar(menuBar) class MyApp(wx.App): def OnInit(self): frame = MyFrame(None, -1, "Hello from wx.Python") frame.Show(True) self.SetTopWindow(frame) return True app = MyApp(0) app.MainLoop()
PyGTK[editar]
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()
PyQt[editar]
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.
O primeiro programa é o tradicional "Hello, world!" ou "Alô, mundo!", e mostra a facilidade de escrever na versão 4 do Qt.
#!/usr/bin/env python import sys from PyQt4 import Qt # We instantiate a QApplication passing the arguments of the script to it: a = Qt.QApplication(sys.argv) # Add a basic widget to this application: # The first argument is the text we want this QWidget to show, the second # one is the parent widget. Since Our "hello" is the only thing we use (the # so-called "MainWidget", it does not have a parent. hello = Qt.QLabel("<h2>Hello, World</h2>") # ... and that it should be shown. hello.show() # Now we can start it. a.exec_()
Esta é uma versão um pouco mais complicada do "Hello World":
import sys from qt import * class HelloButton(QPushButton): def __init__(self, *args): QPushButton.__init__(self, *args) self.setText("Hello World") class HelloWindow(QMainWindow): def __init__(self, *args): 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)
Pygame[editar]
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()