简体   繁体   English

Python-PyQt4 QTextEdit

[英]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. 大家好,我有一个问题我必须编写python脚本,该脚本将首先显示带有按钮文本编辑和一些标签的qt4窗口。 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 | 追溯(最近一次通话):在Koniec QtGui.QMessageBox.Yes中的文件“ Skrypt.py”,第18行。 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' QtGui.QMessageBox.No)类型错误:参数与任何重载调用均不匹配:问题(QWidget,str,str,按钮:Union [QMessageBox.StandardButtons,QMessageBox.StandardButton] = QMessageBox.Ok,defaultButton:QMessageBox.StandardButton = QMessageBox.NoButton ):参数1具有意外类型'bool'问题(QWidget,str,str,int,button1:int = 0,button2:int = 0):参数1具有意外类型'bool'问题(QWidget,str,str,str,str ,button1Text:str ='',button2Text:str ='',defaultButtonNumber:int = 0,escapeButtonNumber:int = -1):参数1具有意外类型'bool'

when i click on right which will be a pylab script which will get value from text edit it displays this : 当我单击右边的那将是一个pylab脚本,该脚本将从文本编辑中获取价值时显示以下内容:

File "Skrypt.py", line 26, in Zatwierdz Zatwierdz中的文件“ Skrypt.py”,第26行
a = int(uiplot.textEdit()) 一个= int(uiplot.textEdit())
TypeError: 'QTextEdit' object is not callable TypeError:“ QTextEdit”对象不可调用

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 我不知道如何解决这个问题,因为这是我第一次进入qt4,所以如果有人可以帮助我,我将不胜感激

Here is the code from script which will display the function using matplotlib 这是脚本中的代码,它将使用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. 单击的信号具有一个布尔值作为可选参数,该布尔值指示是否选中了该按钮(通常为False,因为默认情况下该属性处于禁用状态,在这种情况下,您将第一个参数作为函数的自变量放置) 。您对self的使用感到困惑,在类中使用了那种类型的属性,但是您只有函数,因此不必声明该属性,除了将self传递给QMessageBox还可以将gui与lambda函数的帮助。

  • 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() . 另一个错误是textEditUi_MainWindow类的成员,它不是函数或可调用的东西,因此您不应使用括号, textEditQTextEdit类的对象,因此,如果要获取文本,则必须使用其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. 另一个错误是不验证是否可以完成将要转换为整数的值,一个简单的解决方案是使用try和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. 不建议使用global,因为很难监视它们的行为,因此,如果您是由global变量引起的问题,则将很难检测到它。

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_())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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