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

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)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值