简体   繁体   中英

Python-PyQt4 QTextEdit

Hello Guys I have a question i have to write python script that will first display a qt4 window with push buttons text edit and some labels. Push buttons i have linked but when i click on my left button which is end of script it displays this:

Traceback (most recent call last): File "Skrypt.py", line 18, in Koniec QtGui.QMessageBox.Yes | QtGui.QMessageBox.No) TypeError: arguments did not match any overloaded call: question(QWidget, str, str, buttons: Union[QMessageBox.StandardButtons, QMessageBox.StandardButton] = QMessageBox.Ok, defaultButton: QMessageBox.StandardButton = QMessageBox.NoButton): argument 1 has unexpected type 'bool' question(QWidget, str, str, int, button1: int = 0, button2: int = 0): argument 1 has unexpected type 'bool' question(QWidget, str, str, str, button1Text: str = '', button2Text: str = '', defaultButtonNumber: int = 0, escapeButtonNumber: int = -1): argument 1 has unexpected type 'bool'

when i click on right which will be a pylab script which will get value from text edit it displays this :

File "Skrypt.py", line 26, in Zatwierdz
a = int(uiplot.textEdit())
TypeError: 'QTextEdit' object is not callable

i don't know how to fix that because this is my first time in qt4 so if anyone could help me i will appreciate that

Here is the code from script which will display the function using matplotlib

import skrypt1
import pylab
import skrypt1
from pylab import *

import sys

from PyQt4 import QtGui, QtCore

if __name__ == "__main__":

    app = QtGui.QApplication(sys.argv)

    def Koniec(self):
        global wybor
        wybor = QtGui.QMessageBox.question(self, 'Koniec',
            "Na Pewno chcesz wyjsc ?",
        QtGui.QMessageBox.Yes | QtGui.QMessageBox.No)
        if wybor == QtGui.QMessageBox.Yes: 
            sys.exit()
        else:
            pass

    def Zatwierdz(self):
        x = pylab.arange(-10, 10.5, 0.5)  
        a = int(uiplot.textEdit())
        y1 = [i / -3 + a for i in x if i <= 0]
        y2 = [i**2 / 3 for i in x if i >= 0]
        x1 = [i for i in x if i <= 0]
        x2 = [i for i in x if i >= 0]
        pylab.plot(x1, y1, x2, y2)
        pylab.title('Wykres f(x)')
        pylab.grid(True)
        pylab.show()

    gui_plot = skrypt1.QtGui.QMainWindow()
    uiplot = skrypt1.Ui_MainWindow()
    uiplot.setupUi(gui_plot)

    uiplot.Zatwierdz.clicked.connect(Zatwierdz)
    uiplot.Wyjscie.clicked.connect(Koniec)

    gui_plot.show()
    sys.exit(app.exec_())

Here is the code how window is created :

from PyQt4 import QtCore, QtGui

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(800, 600)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.label = QtGui.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(50, 60, 361, 21))
        self.label.setObjectName(_fromUtf8("label"))
        self.label_2 = QtGui.QLabel(self.centralwidget)
        self.label_2.setGeometry(QtCore.QRect(50, 270, 361, 21))
        self.label_2.setObjectName(_fromUtf8("label_2"))
        self.textEdit = QtGui.QTextEdit(self.centralwidget)
        self.textEdit.setGeometry(QtCore.QRect(40, 360, 711, 71))
        self.textEdit.setObjectName(_fromUtf8("textEdit"))
        self.Zatwierdz = QtGui.QPushButton(self.centralwidget)
        self.Zatwierdz.setGeometry(QtCore.QRect(560, 500, 200, 28))
        self.Zatwierdz.setObjectName(_fromUtf8("Zatwierdz"))
        self.Wyjscie = QtGui.QPushButton(self.centralwidget)
        self.Wyjscie.setGeometry(QtCore.QRect(350, 500, 201, 28))
        self.Wyjscie.setObjectName(_fromUtf8("Wyjscie"))
        self.label_3 = QtGui.QLabel(self.centralwidget)
        self.label_3.setGeometry(QtCore.QRect(50, 160, 701, 31))
        self.label_3.setObjectName(_fromUtf8("label_3"))
        MainWindow.setCentralWidget(self.centralwidget)

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

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
        self.label.setText(_translate("MainWindow", "Funkcja Trygonometryczna", None))
        self.label_2.setText(_translate("MainWindow", "Podaj wspolczynnik a: ", None))
        self.Zatwierdz.setText(_translate("MainWindow", "Zatwierdz", None))
        self.Wyjscie.setText(_translate("MainWindow", "Wyjscie", None))
        self.label_3.setText(_translate("MainWindow", "Skrypt wyswietli wykres funkcji po wpisaniu wartosci ", 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_())

Your code has the following errors:

  • the clicked signal has as optional parameter a Boolean value that indicates if the button is checked or not (it is generally False because that property is disabled by default, and in your case you place as first parameter a self of the functions, that is unnecessary. You are confusing the use of self, that type of attributes is used in the classes, but you only have functions, so it is not necessary to declare that attribute, instead of passing the self to the QMessageBox you can pass the gui with the help of a lambda function.

  • Another error that has is that textEdit is a member of the Ui_MainWindow class, it is not a function or something callable so you should not use parentheses, textEdit is an object of the class QTextEdit so if you want to get the text you must use its method toPlainTex() .

  • Another error is not to verify that the value that is going to be converted to integer can be done, a simple solution is to use try and except.

  • It is not advisable to use global because it is difficult to monitor their behavior, so if you have a problem caused by global variables you will have trouble detecting it.

With all the above I have implemented the following solution:

import sys

from PyQt4 import QtGui, QtCore
import pylab

import skrypt1

def Koniec(gui):
    wybor = QtGui.QMessageBox.question(gui, 'Koniec',
        "Na Pewno chcesz wyjsc ?",
        QtGui.QMessageBox.Yes | QtGui.QMessageBox.No)
    if wybor == QtGui.QMessageBox.Yes: 
        sys.exit()

def Zatwierdz():
    try:
        x = pylab.arange(-10, 10.5, 0.5)  
        a = int(uiplot.textEdit.toPlainText())
        y1 = [i / -3 + a for i in x if i <= 0]
        y2 = [i**2 / 3 for i in x if i >= 0]
        x1 = [i for i in x if i <= 0]
        x2 = [i for i in x if i >= 0]
        pylab.plot(x1, y1, x2, y2)
        pylab.title('Wykres f(x)')
        pylab.grid(True)
        pylab.show(block=False)
    except ValueError:
        print("Error")


if __name__ == "__main__":

    app = QtGui.QApplication(sys.argv)

    gui_plot = skrypt1.QtGui.QMainWindow()
    uiplot = skrypt1.Ui_MainWindow()
    uiplot.setupUi(gui_plot)

    uiplot.Zatwierdz.clicked.connect(Zatwierdz)
    uiplot.Wyjscie.clicked.connect(lambda checked, w=gui_plot:Koniec(gui_plot))

    gui_plot.show()
    sys.exit(app.exec_())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM