gpt4 book ai didi

python - 带有嵌入式 Matplotlib 的 PyQt - 焦点问题

转载 作者:太空宇宙 更新时间:2023-11-03 21:41:53 24 4
gpt4 key购买 nike

我想创建一个 GUI,左侧是用户输入,右侧是绘图。 应用按钮应根据输入更新绘图。现在该按钮只是清除 TextLabel。

我用 Qt Desginer 做了一个基本的布局并尝试实现 this示例。

如果没有绘图,可以使用鼠标聚焦不同的 QLineEdit 来工作,但是,如果绘图聚焦仅适用于键盘的选项卡,而不能再单击。

MyMplCanvas 类是否捕获了鼠标焦点或者为什么它不起作用?

这是代码:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from __future__ import unicode_literals
import random
import matplotlib
# Make sure that we are using QT5
matplotlib.use('Qt5Agg')
from PyQt5 import QtCore, QtWidgets
from numpy import arange, sin, pi
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure


class MyMplCanvas(FigureCanvas):
"""Ultimately, this is a QWidget (as well as a FigureCanvasAgg, etc.)."""

def __init__(self, parent=None, width=5, height=4, dpi=100):
fig = Figure(figsize=(width, height), dpi=dpi)
self.axes = fig.add_subplot(111)

self.compute_initial_figure()

FigureCanvas.__init__(self, fig)
self.setParent(parent)

FigureCanvas.setSizePolicy(self,
QtWidgets.QSizePolicy.Expanding,
QtWidgets.QSizePolicy.Expanding)
FigureCanvas.updateGeometry(self)

def compute_initial_figure(self):
pass


class MyStaticMplCanvas(MyMplCanvas):
"""Simple canvas with a sine plot."""

def compute_initial_figure(self):
t = arange(0.0, 3.0, 0.01)
s = sin(2*pi*t)
self.axes.plot(t, s)


class MyDynamicMplCanvas(MyMplCanvas):
"""A canvas that updates itself every second with a new plot."""

def __init__(self, *args, **kwargs):
MyMplCanvas.__init__(self, *args, **kwargs)
timer = QtCore.QTimer(self)
timer.timeout.connect(self.update_figure)
timer.start(1000)

def compute_initial_figure(self):
self.axes.plot([0, 1, 2, 3], [1, 2, 0, 4], 'r')

def update_figure(self):
# Build a list of 4 random integers between 0 and 10 (both inclusive)
l = [random.randint(0, 10) for i in range(4)]
self.axes.cla()
self.axes.plot([0, 1, 2, 3], l, 'r')
self.draw()

class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(852, 678)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout_2 = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout_2.setObjectName("gridLayout_2")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.formLayout = QtWidgets.QFormLayout()
self.formLayout.setContentsMargins(5, 5, 5, 5)
self.formLayout.setObjectName("formLayout")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setObjectName("label")
self.formLayout.setWidget(0, QtWidgets.QFormLayout.LabelRole, self.label)
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setObjectName("lineEdit")
self.formLayout.setWidget(0, QtWidgets.QFormLayout.FieldRole, self.lineEdit)
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setObjectName("label_2")
self.formLayout.setWidget(1, QtWidgets.QFormLayout.LabelRole, self.label_2)
self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_2.setObjectName("lineEdit_2")
self.formLayout.setWidget(1, QtWidgets.QFormLayout.FieldRole, self.lineEdit_2)
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setObjectName("label_3")
self.formLayout.setWidget(2, QtWidgets.QFormLayout.LabelRole, self.label_3)
self.lineEdit_3 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_3.setObjectName("lineEdit_3")
self.formLayout.setWidget(2, QtWidgets.QFormLayout.FieldRole, self.lineEdit_3)
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setMinimumSize(QtCore.QSize(200, 0))
self.pushButton.setObjectName("pushButton")
self.formLayout.setWidget(3, QtWidgets.QFormLayout.SpanningRole, self.pushButton)
self.horizontalLayout_2.addLayout(self.formLayout)
self.gridLayout = QtWidgets.QGridLayout()
self.gridLayout.setContentsMargins(5, 5, 5, 5)
self.gridLayout.setObjectName("gridLayout")
self.widget_2 = QtWidgets.QWidget(self.centralwidget)
self.widget_2.setMinimumSize(QtCore.QSize(300, 300))
self.widget_2.setObjectName("widget_2")

#self.gridLayout.addWidget(self.widget_2, 0, 1, 1, 1)
self.gridLayout.addWidget(MyDynamicMplCanvas(self.widget_2), 0, 1, 1, 1)

self.widget_1 = QtWidgets.QWidget(self.centralwidget)
self.widget_1.setMinimumSize(QtCore.QSize(300, 300))
self.widget_1.setObjectName("widget_1")

#self.gridLayout.addWidget(self.widget_1, 0, 0, 1, 1)
self.gridLayout.addWidget(MyStaticMplCanvas(self.widget_1), 0, 0, 1, 1)

self.widget_4 = QtWidgets.QWidget(self.centralwidget)
self.widget_4.setMinimumSize(QtCore.QSize(300, 300))
self.widget_4.setObjectName("widget_4")
self.gridLayout.addWidget(self.widget_4, 1, 1, 1, 1)
self.widget_3 = QtWidgets.QWidget(self.centralwidget)
self.widget_3.setMinimumSize(QtCore.QSize(300, 300))
self.widget_3.setObjectName("widget_3")
self.gridLayout.addWidget(self.widget_3, 1, 0, 1, 1)
self.horizontalLayout_2.addLayout(self.gridLayout)
self.gridLayout_2.addLayout(self.horizontalLayout_2, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 852, 20))
self.menubar.setObjectName("menubar")
self.menuDatei = QtWidgets.QMenu(self.menubar)
self.menuDatei.setObjectName("menuFile")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.menubar.addAction(self.menuDatei.menuAction())

self.retranslateUi(MainWindow)
self.pushButton.clicked.connect(self.label.clear)
QtCore.QMetaObject.connectSlotsByName(MainWindow)

def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "TextLabel"))
self.label_2.setText(_translate("MainWindow", "TextLabel"))
self.label_3.setText(_translate("MainWindow", "TextLabel"))
self.pushButton.setText(_translate("MainWindow", "Apply"))
self.menuDatei.setTitle(_translate("MainWindow", "File"))


if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())

最佳答案

该问题是由位于布局顶部的 self.widget_1 和 self.widget_2 引起的,阻止了鼠标事件的传播。

这个想法是用 Canvas 替换 self.widget_1 和 self.widget_2 ,如下所示:

...
self.gridLayout = QtWidgets.QGridLayout()
self.gridLayout.setContentsMargins(5, 5, 5, 5)
self.gridLayout.setObjectName("gridLayout")
self.widget_2 = MyDynamicMplCanvas(self.centralwidget) # <---
self.widget_2.setMinimumSize(QtCore.QSize(300, 300))
self.widget_2.setObjectName("widget_2")
self.gridLayout.addWidget(self.widget_2, 0, 1, 1, 1)
# self.gridLayout.addWidget(MyDynamicMplCanvas(self.widget_2), 0, 1, 1, 1)
self.widget_1 = MyStaticMplCanvas(self.centralwidget) # <---
self.widget_1.setMinimumSize(QtCore.QSize(300, 300))
self.widget_1.setObjectName("widget_1")
self.gridLayout.addWidget(self.widget_1, 0, 0, 1, 1)
# self.gridLayout.addWidget(MyStaticMplCanvas(self.widget_1), 0, 0, 1, 1)
self.widget_4 = QtWidgets.QWidget(self.centralwidget)
self.widget_4.setMinimumSize(QtCore.QSize(300, 300))
self.widget_4.setObjectName("widget_4")
self.gridLayout.addWidget(self.widget_4, 1, 1, 1, 1)
self.widget_3 = QtWidgets.QWidget(self.centralwidget)
self.widget_3.setMinimumSize(QtCore.QSize(300, 300))
self.widget_3.setObjectName("widget_3")
self.gridLayout.addWidget(self.widget_3, 1, 0, 1, 1)
...

关于python - 带有嵌入式 Matplotlib 的 PyQt - 焦点问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52794962/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com