如何从 QLineEdit() 获取 int? [英] How to get int from QLineEdit()?

查看:51
本文介绍了如何从 QLineEdit() 获取 int?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这是我的代码,我尝试了很多方法来更改 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: '>' 在 'QLineEdit' 和 'QLineEdit' 的实例之间不受支持

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

我想设置用户希望的轴限制,但我不知道发生了什么.我尝试的是 QRegValidator,通过使用 int 函数、float 函数和设置轴限制的不同方式,但总是出现此错误.

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.

推荐答案

您不应该使用 QLinEdit 来获取数字,而是通过 text 方法获取 QLineEdit 中包含的文本.然后使用 int 或 float 方法将其转换为数字.将字符串转换为数字可能会引发错误,例如,如果文本为空,则无法将其转换为数字,等等其他情况,但是当您使用验证器时,这会消除除空字符串之外的所有情况,因此它转换前必须进行验证.

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.

另一方面,你不应该在构造函数中这样做,因为那时 QLineEdit 是空的.

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

注意:删除self.ax.set_ylim(self.range_from2, self.range_to2).

这篇关于如何从 QLineEdit() 获取 int?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆