参考文章:pyQt 写的小窗口 获取文本框内容 - 荒凉的废墟的日志 - 网易博客
http://huangliangfeixu.blog.163.com/blog/static/1897470622011216105210137/
主要解决这篇文章代码输出中文乱码的现象。在“中文内容”前加u,即----u“中文内容”
这个例子不仅实现了将文本框的内容输出,还有文本框的布局内容也同样值得作参考。
# -*- coding: cp936 -*-
import sys
from PyQt4 import QtGui,QtCore
class MainWindow(QtGui.QWidget):
def __init__(self,parent=None):
QtGui.QWidget.__init__(self,parent)
self.setWindowTitle(u'酒店管理')
self.setWindowIcon(QtGui.QIcon('fire.jpg'))
houseNum = QtGui.QLabel(u'房价号')
price = QtGui.QLabel(u'房款')
yajin = QtGui.QLabel(u'押金')
ruzhu = QtGui.QLabel(u'入住')
qiandan = QtGui.QLabel(u'签单')
date = QtGui.QLabel(u'日期')
self.houseNumEdit = QtGui.QLineEdit()
self.priceEdit = QtGui.QLineEdit()
self.yajinEdit = QtGui.QLineEdit()
self.ruzhuEdit = QtGui.QLineEdit()
self.qiandanEdit = QtGui.QLineEdit()
self.dateEdit = QtGui.QLineEdit()
grid =QtGui.QGridLayout()
grid.addWidget(houseNum,1,0)
grid.addWidget(self.houseNumEdit,1,1)
grid.addWidget(price,2,0)
grid.addWidget(self.priceEdit,2,1)
grid.addWidget(yajin,3,0)
grid.addWidget(self.yajinEdit,3,1)
grid.addWidget(ruzhu,4,0)
grid.addWidget(self.ruzhuEdit,4,1)
grid.addWidget(qiandan,5,0)
grid.addWidget(self.qiandanEdit,5,1)
grid.addWidget(date,6,0)
grid.addWidget(self.dateEdit,6,1)
ok = QtGui.QPushButton(u'确定')
grid.addWidget(ok,7,0)
self.setLayout(grid)
self.resize(550,400)
self.connect(ok,QtCore.SIGNAL('clicked()'), self.addNum) #OK按钮 的clicked()时间 信号 绑定到addNum这个函数 也叫槽
def addNum(self):
houseNum = self.houseNumEdit.text() #获取文本框内容
yajin = self.yajinEdit.text()
price = self.priceEdit.text()
ruzhu = self.ruzhuEdit.text()
qiandan = self.qiandanEdit.text()
date = self.dateEdit.text()
print(u'房间号: %s 押金: %s 房款: %s 入住: %s 签单: %s 日期: %s' % (houseNum,
yajin,price,ruzhu,
qiandan,date))
app = QtGui.QApplication(sys.argv)
main = MainWindow()
main.show()
sys.exit(app.exec_())
#!/usr/bin/python
# -*- coding: utf-8 -*-
#单行编辑器中键入的文字会立即打印在屏幕上
from PyQt4 import QtGui
from PyQt4 import QtCore
class Example(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.initUI()
def initUI(self):
self.edit = QtGui.QLineEdit(self)#创建 QLineEdit 。
self.edit.move(60, 100)#文本框的位置
self.connect(self.edit, QtCore.SIGNAL('textChanged(QString)'),self.onChanged)
#如果单行编辑器中的文本发生变化,调用 onChanged() 方法。
self.setWindowTitle('QLineEdit')
self.setGeometry(250, 200, 350, 250)
def onChanged(self):#这个函数能够实时打印文本框的内容
b=self.edit.text()
print (u'文本框此刻输入的内容是:%s'%b)
def main():
app = QtGui.QApplication([])
exm = Example()
exm.show()
app.exec_()
if __name__ == '__main__':
main()