pyqt4教程之widget使用示例分享


复制代码 代码如下:

-- coding: utf-8 --

import sys
from PyQt4 import QtCore, QtGui
class MyWindow(QtGui.QWidget):
def init(self, parent=None):
QtGui.QWidget.init(self,parent )
self.setWindowTitle("weather")
self.resize(1000, 200)
girdLayout = QtGui.QGridLayout()

标签

str ="weather"
label1 = QtGui.QLabel(str)
girdLayout.addWidget( label1, 0, 0)

按钮

button1 = QtGui.QPushButton('button1')
girdLayout.addWidget ( button1 , 0, 1)

文本条

textFile = QtGui.QLineEdit()
girdLayout.addWidget( textFile, 0, 2 )

密码条

passwordFile = QtGui.QLineEdit()
passwordFile.setEchoMode( QtGui.QLineEdit.Password )
girdLayout.addWidget( passwordFile, 1, 2)

编辑框

textArea = QtGui.QTextEdit()
girdLayout.addWidget(textArea , 2, 2 )

单选框&复选框

self.radio1 = QtGui.QRadioButton('radio1')
self.radio2 = QtGui.QRadioButton('radio2')
self.radio3 = QtGui.QRadioButton('radio3')
girdLayout.addWidget( self.radio1 , 3 ,0)
girdLayout.addWidget( self.radio2 , 3 ,1)
girdLayout.addWidget( self.radio3 , 3 ,2)
checkbox1 = QtGui.QCheckBox('checkbox1')
checkbox2 = QtGui.QCheckBox('checkbox2')
checkbox3 = QtGui.QCheckBox('checkbox2')
girdLayout.addWidget( checkbox1 , 4, 0)
girdLayout.addWidget( checkbox2 , 4, 1)
girdLayout.addWidget( checkbox3 , 4, 2)

self.button = QtGui.QPushButton('ok')
girdLayout.addWidget( self.button, 5 , 0)

self.connect(self.button ,QtCore.SIGNAL('clicked()'),self.OnButton )
self.setLayout( girdLayout)

def OnButton(self ):
if self.radio2.isChecked():
self.radio2.setText('haha')

app = QtGui.QApplication( sys.argv )
mywindow = MyWindow()
mywindow.show()
app.exec_()