将多个lineEdit槽中的字符串作为变量复制到PyQt4中的一个textEdit槽

时间:2010-10-02 12:20:58

标签: python pyqt4

为了更清楚,这里的事情可能会如何运作。

在python中,要创建一个变量,我们只需使用

var1 = raw_input('your name?') 

这样当使用

print 'your name is ' +var1

它将打印存储在var1中的字符串。

问题是如何使用Pyqt4制作它?我有3个lineEdit符号作为名称,年龄和性别,还有一个textEdit用于将lineEdit中的字符串打印到此textEdit中。这就像制作电话簿一样。可能吗?代码如何?我急切地想知道答案..

在这里,我提供了一些使事情更清晰的来源。

这是ui:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'phonebook.ui'
#
# Created: Sat Oct  2 15:18:52 2010
#      by: PyQt4 UI code generator 4.7.2
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

class Ui_phonebook(object):
    def setupUi(self, phonebook):
        phonebook.setObjectName("phonebook")
        phonebook.resize(240, 300)
        phonebook.setMinimumSize(QtCore.QSize(240, 300))
        phonebook.setMaximumSize(QtCore.QSize(240, 300))
        self.label = QtGui.QLabel(phonebook)
        self.label.setGeometry(QtCore.QRect(20, 30, 57, 14))
        self.label.setObjectName("label")
        self.label_2 = QtGui.QLabel(phonebook)
        self.label_2.setGeometry(QtCore.QRect(20, 60, 57, 14))
        self.label_2.setObjectName("label_2")
        self.lineEdit = QtGui.QLineEdit(phonebook)
        self.lineEdit.setGeometry(QtCore.QRect(110, 30, 113, 20))
        self.lineEdit.setObjectName("lineEdit")
        self.lineEdit_2 = QtGui.QLineEdit(phonebook)
        self.lineEdit_2.setGeometry(QtCore.QRect(110, 60, 113, 20))
        self.lineEdit_2.setObjectName("lineEdit_2")
        self.textEdit = QtGui.QTextEdit(phonebook)
        self.textEdit.setGeometry(QtCore.QRect(20, 142, 201, 141))
        self.textEdit.setObjectName("textEdit")
        self.pushButton = QtGui.QPushButton(phonebook)
        self.pushButton.setGeometry(QtCore.QRect(70, 100, 89, 23))
        self.pushButton.setObjectName("pushButton")

        self.retranslateUi(phonebook)
        QtCore.QMetaObject.connectSlotsByName(phonebook)

    def retranslateUi(self, phonebook):
        phonebook.setWindowTitle(QtGui.QApplication.translate("phonebook", "Simple Phonebook", None, QtGui.QApplication.UnicodeUTF8))
        self.label.setText(QtGui.QApplication.translate("phonebook", "Name:", None, QtGui.QApplication.UnicodeUTF8))
        self.label_2.setText(QtGui.QApplication.translate("phonebook", "E-mail:", None, QtGui.QApplication.UnicodeUTF8))
        self.lineEdit.setText(QtGui.QApplication.translate("phonebook", "lineEdit", None, QtGui.QApplication.UnicodeUTF8))
        self.lineEdit_2.setText(QtGui.QApplication.translate("phonebook", "lineEdit_2", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton.setText(QtGui.QApplication.translate("phonebook", "Preview", None, QtGui.QApplication.UnicodeUTF8))

这是主要来源:

#! /usr/bin/env python

import sys
from PyQt4 import QtCore, QtGui
from phonebook import Ui_phonebook

class Main(QtGui.QDialog):
  def __init__(self, parent=None):
    QtGui.QWidget.__init__(self, parent)
    self.ui = Ui_phonebook()
    self.ui.setupUi(self)
    self.center()

    QtCore.QObject.connect(self.ui.pushButton, QtCore.SIGNAL('clicked()'), self.preview)

  def preview(self):
    lineEdit = '"content inside lineEdit"'
    lineEdit_2 = '"content inside lineEdit_2"'
    self.ui.textEdit.setText('Name: ' +lineEdit +'\n\nEmail: ' +lineEdit_2)

  def center(self):
    screen = QtGui.QDesktopWidget().screenGeometry()
    size =  self.geometry()
    self.move((screen.width()-size.width())/2, (screen.height()-size.height())/2)


if __name__ == '__main__':
  app = QtGui.QApplication(sys.argv)
  main = Main()
  main.show()
  sys.exit(app.exec_())

2 个答案:

答案 0 :(得分:0)

当在Qt(和PyQt)中单击按钮时,发出信号。您可以将此信号连接到任何插槽(在PyQt中可以是任何Python方法)并在该插槽中执行任何操作 - 例如查看3个框中的文本并打印一些内容。

例如,假设您创建了一个按钮:

    self.start_button = QPushButton("&Start")

连接其clicked信号:

    self.connect(self.start_button, SIGNAL('clicked()'), self.on_start)

到您班级的on_start方法。在这个方法中,调用其他小部件的相关方法来做任何你想做的事。

答案 1 :(得分:0)

我已经解开了这个谜团..好吧,我必须插入的是text()!!

def preview(self):
  lineEdit = self.ui.lineEdit.text()
  lineEdit_2 = self.ui.lineEdit_2.text()
  self.ui.textEdit.setText('Name: ' +lineEdit +'\n\nEmail: ' +lineEdit_2)

是的..这应该让我的梦想成真..感谢所有让efford帮助我的人.. = ^ _ ^ =