PyQt 第一个程序 “Hello World”

# Every Qt application needs a QApplication object. sys.argv are the command line arguments.
# QApplication needs them so it can handle options, like -help (try it) a=qt.QApplication(sys.argv)
# We create a button called w. It says "Hello World" and has no parent (None as second argument).
# Not having a parent means it's a top-level window, so it's not "inside" anything.
# In other words: a window with a button as its whole content. w=qt.QPushButton("Hello World",None)
# Show the button w.show()
# Enter the application loop. That means the application will start running,
# and will start to interact with the user.
# Since there is nothing to interact with, no user-input can stop the application from 'running'.
# The fully functional one below doesn't have this limitation and uses the main concept of PyQT. a.exec_loop() }}}

#import qt,sys
#a=qt.QApplication(sys.argv)
#w=qt.QPushButton("Hello World",None)

# Here the differences to the trivial one above may be seen, using QT's signal and slot features:
# We connect the clicked() signal to the button's "close" slot (method).
# Whenever you click on the button, it emits the signal. Then every slot that is
# connected to that signal gets called. In this case, only w.close w.connect(w,qt.SIGNAL("clicked()"),w.close)
# We make w the main widget of the application. That means that when w is closed
# The application exits. If we didn't do this, we would have to call a.quit()
# to make it end. a.setMainWidget(w)

#w.show()
#a.exec_loop()

import sys;
from PyQt4 import QtGui,QtCore
from PyQt4.QtCore import *;
from PyQt4.QtGui import *;
 
class MainWindow(QDialog):
        def __init__(self, parent=None):
                #QMainWindow.__init__(self, parent)
                #self.setWindowTitle(self.tr("Hello World From PyQt!"))
                btnpush.connect(btnpush, SIGNAL("clicked()"), btnpush.close)
 
if __name__ == "__main__":
        app = QApplication(sys.argv)
        btnpush = QPushButton("Hello World", None)
        btnpush.show()
        #mainwindow = MainWindow()
        #mainwindow.show()
        sys.exit(app.exec_())

 

 

import sys;
from PyQt4.QtCore import *;
from PyQt4.QtGui import *;

class MainWindow(QMainWindow):
        def __init__(self, parent=None):
                QMainWindow.__init__(self, parent)
                self.setWindowTitle(self.tr("Hello World From PyQt!"))


if __name__ == "__main__":
        app = QApplication(sys.argv)
        mainwindow = MainWindow()
        mainwindow.show()
        sys.exit(app.exec_())

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值