简体   繁体   中英

How to get int from QLineEdit()?

So here is my code and I tried many ways of changing the input of QLineEdit, but nothing is working, only thing I'm getting is

TypeError: '>' not supported between instances of 'QLineEdit' and 'QLineEdit'

from matplotlib import pyplot
import numpy as np
from PyQt5.QtWidgets import (QWidget, QPushButton,
                             QLabel, QGridLayout,
                             QLineEdit, QApplication,
                             QListWidget, QListWidgetItem)
from PyQt5.QtGui import QIntValidator, QRegExpValidator

import sys


class Program(QWidget):

    def __init__(self):
        super().__init__()

        self.x_pts = []
        self.y_pts = []

        self.fig, self.ax = pyplot.subplots()
        self.points, = self.ax.plot(self.x_pts, self.y_pts, marker="o", linestyle="")

        self.label1 = QLabel('Rozpocznij wyznaczanie' + 2*'')
        self.button1 = QPushButton('Start')
        self.label2 = QLabel('Wprowadź zakres x')
        self.label3 = QLabel('Wprowadź zakres y')
        self.label4 = QLabel('Od')
        self.label5 = QLabel('Do')
        self.label6 = QLabel('Od')
        self.label7 = QLabel('Do')
        self.range_from1 = QLineEdit()
        self.range_to1 = QLineEdit()
        self.range_from2 = QLineEdit()
        self.range_to2 = QLineEdit()
        self.onlyInt = QIntValidator(-100, 100)
        self.range_from1.setValidator(QRegExpValidator(
            QRegExp('^(-(100|([1-9]{1}[0-9]{1})|[1-9]{1}))|(0)$')))
        self.range_from2.setValidator(self.onlyInt)
        self.range_to1.setValidator(QRegExpValidator(
            QRegExp('^(-(100|([1-9]{1}[0-9]{1})|[1-9]{1}))|(0)$')))
        self.range_to2.setValidator(self.onlyInt)

        self.ax.set_ylim(self.range_from2, self.range_to2)

        siatka = QGridLayout()
        siatka.setSpacing(10)

        siatka.addWidget(self.label1, 0, 0)
        siatka.addWidget(self.label2, 1, 0)
        siatka.addWidget(self.label4, 2, 0)
        siatka.addWidget(self.range_from1, 2, 1)
        siatka.addWidget(self.label5, 3, 0)
        siatka.addWidget(self.range_to1, 3, 1)
        siatka.addWidget(self.label3, 4, 0)
        siatka.addWidget(self.label6, 5, 0)
        siatka.addWidget(self.range_from2, 5, 1)
        siatka.addWidget(self.label7, 6, 0)
        siatka.addWidget(self.range_to2, 6, 1)
        siatka.addWidget(self.button1, 7, 0)

        self.setLayout(siatka)

        self.button1.clicked.connect(self.showing_canvas)

        self.show()

    def wyznaczanie(self, event):
        m_x, m_y = event.x, event.y
        x, y = self.ax.transData.inverted().transform([m_x, m_y])
        self.x_pts.append(x)
        self.y_pts.append(y)
        self.points.set_xdata(self.x_pts)
        self.points.set_ydata(self.y_pts)
        self.fig.canvas.draw()

    def showing_canvas(self):
        self.fig.canvas.mpl_connect('button_press_event', self.wyznaczanie)
        pyplot.show()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Program()
    sys.exit(app.exec_())

I want to set axis limits as the user wants them to be, and I have no idea what is going on. The things I tried was QRegValidator, by using int function, float function, and different ways of setting axis limits, and yet always this error.

You should not use the QLinEdit to get the number but the text contained in the QLineEdit that is obtained through the text method. Then you use the int or float method to convert it to a number. The conversion of a string to a number can throw an error, for example if the text is empty it cannot be converted to a number, and so on other cases, but as you use a validator this eliminates all cases except the empty string so it will have to verify before conversion.

On the other hand you should not do that in the constructor since at that time the QLineEdit is empty.

def showing_canvas(self):
    text1 = self.range_from2.text().replace(",", ".")
    text2 = self.range_to2.text().replace(",", ".")
    y1 = float(text1) if text1 else 0
    y2 = float(text2) if text2 else 0
    self.ax.set_ylim(y1, y2)
    self.fig.canvas.mpl_connect('button_press_event', self.wyznaczanie)
    pyplot.show()

Note: remove self.ax.set_ylim(self.range_from2, self.range_to2) .

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