如何判断在pyQt4 lineEdit中输入/返回的时间

时间:2016-10-11 00:06:09

标签: python python-3.x pyqt pyqt4

我遇到问题,我无法弄清楚如何判断用户是否输入。我读过的所有stackoverflow帖子和文档都告诉我要添加self.textEdit.returnPressed.connect,或类似的东西,没有任何解决方案有效。我使用pyQt 4.这里是代码:

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

# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!

import gi
import signal
gi.require_version('Gtk', '3.0')
import sys
import dbus
import pygtk
import gi
import signal
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import pyqtSlot
from PyQt4.QtCore import QUrl
from PyQt4.QtWebKit import QWebView

import sip
sip.setapi('QString', 2)
sip.setapi('QVariant', 2)

gi.require_version('Notify', '0.7')


try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(679, 600)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))

        self.saveButton = QtGui.QPushButton(self.centralwidget)
        self.saveButton.setGeometry(QtCore.QRect(10, 0, 88, 28))
        self.saveButton.setObjectName(_fromUtf8("pushButton"))

        self.textEdit = QtGui.QTextEdit(self.centralwidget)
        self.textEdit.setGeometry(QtCore.QRect(0, 30, 681, 800))
        self.textEdit.setObjectName(_fromUtf8("textEdit"))

        self.fontButton = QtGui.QPushButton(self.centralwidget)
        self.fontButton.setGeometry(QtCore.QRect(400, 0, 88, 28))
        self.fontButton.setObjectName(_fromUtf8("fontButton"))

        self.fontSize = QtGui.QTextEdit(self.centralwidget)
        self.fontSize.setGeometry(QtCore.QRect(200, 0, 88, 28))
        self.fontSize.setObjectName(_fromUtf8("fontEdit"))
        self.fontSize.returnPressed.connect(self.pushButtonOK.click)

        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 679, 24))
        self.menubar.setObjectName(_fromUtf8("menubar"))

        self.menuTest = QtGui.QMenu(self.menubar)
        self.menuTest.setObjectName(_fromUtf8("menuTest"))
        MainWindow.setMenuBar(self.menubar)

        self.statusbar = QtGui.QStatusBar(MainWindow)
        self.statusbar.setObjectName(_fromUtf8("statusbar"))
        MainWindow.setStatusBar(self.statusbar)
        self.menubar.addAction(self.menuTest.menuAction())

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

        @QtCore.pyqtSlot()
        def on_pushButtonOK_clicked(self):
            inputNumber = self.lineEditNumber.text()
            if inputNumber.isdigit():
                info = "You selected `{0}`"

            else:
                info = "Please select a number, `{0}` isn't valid!"

            print(info.format(inputNumber))

        #functions



        def save(self):
            with open('log.txt', 'w') as yourFile:
                yourFile.write(str(self.textEdit.toPlainText()))

        def saveFont(self):
            self.QtGui.QtFont.font.setPointSize(int(self.fontSize))


        def commander(self):
            save(self)

        self.saveButton.clicked.connect(lambda: save(self))
        self.fontButton.clicked.connect(lambda: save(self))



    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
        self.saveButton.setText(_translate("MainWindow", "Save text", None))
        self.fontButton.setText(_translate("MainWindow", "Save Font", None))
        self.menuTest.setTitle(_translate("MainWindow", "test", None))

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    MainWindow = QtGui.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:1)

你必须犯错误

  • returnPressed适用于QtLineEdit,您使用QtTextEdit
  • 您没有self.pushButtonOKself.saveButtonself.fontButton

所以你需要

    self.fontSize = QtGui.QLineEdit(self.centralwidget)
    self.fontSize.setGeometry(QtCore.QRect(200, 0, 88, 28))
    self.fontSize.setObjectName(_fromUtf8("fontEdit"))
    self.fontSize.returnPressed.connect(self.fontButton.click)