您的位置:首页 > 编程语言 > Python开发

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

2004-07-05 19:31 429 查看
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)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: