所以这是我的代码,我尝试了很多方法来更改 QLineEdit 的输入,但没有任何效果,我唯一得到的是
类型错误:“QLineEdit”和“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 函数以及设置轴限制的不同方式,但总是出现此错误。