从多个lineEdit槽复制字符串作为变量到一个textEdit槽的PyQt4实现

0 投票
2 回答
1659 浏览
提问于 2025-04-16 04:53

为了让事情更清楚,这里解释一下它是怎么运作的。

在Python中,创建一个变量很简单,我们只需要用

var1 = raw_input('your name?') 

这样,当你使用

print 'your name is ' +var1

时,它会打印出存储在var1中的字符串。

问题是,如何用Pyqt4来实现这个呢?我有三个输入框,分别代表名字、年龄和性别,还有一个文本框用来把输入框里的内容打印出来。这就像是在制作一个电话簿。这样做可以吗?代码会是什么样子的?我非常想知道答案……

这里我提供一些资料来让事情更清楚。

这是界面:

# -*- 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

我解决了这个谜题……其实,我只需要插入 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)

没错……这应该能让我梦想成真……感谢所有帮助我的人…… =^_^=

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

在Qt(和PyQt)中,当你点击一个按钮时,它会发出一个信号。你可以把这个信号连接到任何一个槽(在PyQt中就是任何一个Python方法),然后在这个槽里做你想做的事情,比如查看三个文本框里的内容,然后打印一些东西。

举个例子,假设你创建了一个按钮,代码是:

    self.start_button = QPushButton("&Start")

你需要把它的clicked信号连接到你类里的on_start方法。然后在这个方法里,调用其他控件的相关方法,完成你想要的操作。

撰写回答