我的環境是python 2.6+PyQt v4.1
PyQt介紹就不說了,這個過程式的代碼可以讓你很好的瞭解PyQt是怎麼回事,這樣來寫程式是不現實的,正真的還是要用物件導向的方法來實現。
My Code注釋用的中文,可能大多數python不支援,如果要啟動並執行話就刪掉注釋,別再IDLE下運行,那樣會存在問題,安裝好python和PyQt後直接在命令列模式下運行。
:
退出後會調用printf函數在命令列下顯示一段話:
import sysfrom PyQt4 import QtGui,QtCoreapp=QtGui.QApplication(sys.argv)def printf():print 'window has closed'#建立視窗window=QtGui.QWidget()#設定視窗大小window.resize(600,400)#或者設定初始位置和大小window.setGeometry(200,200,600,400)#設定視窗標題window.setWindowTitle('hello')#設定視窗左上方表徵圖window.setWindowIcon(QtGui.QIcon('1.jpg'))#預設路徑在代碼同一目錄下#即可寫成window.setWindowIcon(QtGui.QIcon('e:\\python code\\1.jpg'))#設定懸浮window.setToolTip('this is a window')#設定懸浮字型樣式和大小QtGui.QToolTip.setFont(QtGui.QFont('OldEnglish', 10))#顯示window.show()#建立一個按鈕q=QtGui.QPushButton("quit",window)q.setGeometry(500,300,80,40)q.setToolTip('close the window')q.show()#訊號槽q.clicked.connect(printf)q.clicked.connect(QtGui.qApp.quit)sys.exit(app.exec_())