CSDN博客

img yuanqingfei

Python的 两个GUI库(GTK和QT)分别写的Helloworld

发表于2004/7/5 19:31:00  2071人阅读

Some people like examples of the code for a widget toolkit before downloading it. Here is an example of the classic "Hello World" program written in python with PyGTK:

 

import gtk

def hello_cb(button):
 print "Hello World"
 window.destroy()

window = gtk.Window(gtk.WINDOW_TOPLEVEL) # create a top level window
window.connect("destroy", gtk.mainquit)  # quit the event loop on destruction
window.set_border_width(10)              # set padding round child widget

button = gtk.Button("Hello World")
button.connect("clicked", hello_cb)      # call hello_cb when clicked
window.add(button)                       # add button to window
button.show()                            # show button

window.show()
gtk.main()                               # enter the main event loop

 

 

This is the traditional ”Hello World” button application, with as little code as possible:
Hello World
Example 6-1. hello1.py — hello world

#
# hello1.py
#
import sys                                                
from qt import *                                          

app=QApplication(sys.argv)                                
button=QPushButton("Hello World", None)                   
app.setMainWidget(button)                                 
button.show()                                             
app.exec_loop()                                           

 

Example 6-2. hello2.py — a better hello world

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)

 

0 0

相关博文

我的热门文章

img
取 消
img