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)
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)
相关文章推荐
- python 检查IP是否能ping通,并且分别导入两个文件
- C/C++ 两大GUI库Qt & GTK比较
- python 6-5 如何读写excel文件 使用第三方库xlrd 和 xlwt,这两个库分别用于excel读和写
- Windows下两个python版本怎么分别安装库
- C/C++ 两大GUI库Qt & GTK比较
- C/C++ 两大GUI库Qt & GTK比较
- KDE/QT vs GNOME/GTK
- u-boot启动的两个阶段 分别对应start.S和board.c
- /LGC设计模式/GUI 主循环设计及其在 MiniGUI, GTK, QT 的实现
- Python--合并两个有序列表
- Python-Django学习中遇到的两个小问题
- Python中的两个方法string.maketrans()和string.translate()
- Python基础1.2环境搭建和第一个程序helloworld
- Python 中两个字典(dict)合并
- python同时给两个收件人发送邮件的方法
- Python使用difflib对比两个文件操作实例
- 求两个字符串的最长公共子串 python实现
- 著名的自由软件圣战- “KDE/QT .VS. Gnome/Gtk”
- 编写一个应用程序,用户分别从两个文本框输入学术的姓名和分数,程序按成绩排序将这些学生的姓名和分数显示在一个文本区中
- Python:通过计算阶乘来学习lambda和reduce这两个函数的使用