gpt4 book ai didi

python - 动态 Canvas 控制

转载 作者:行者123 更新时间:2023-12-04 09:45:52 25 4
gpt4 key购买 nike

我想根据选定的 QRadiobutton 更改 Figure。在我的真实代码中,我尝试了各种有和没有信号的方法(另外,检查了其他主题。)。
为了使它看起来不那么复杂,我重新排列了我的问题代码。

我希望有人能帮助我克服这种情况。

这是我的代码;
图.py

from PyQt5.QtWidgets import*

from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas

from matplotlib.figure import Figure

from numpy import arange, sin, cos, pi


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

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

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

FigureCanvas.updateGeometry(self)

t = arange(0.0, 5.0, 0.01)
s = sin(2 * pi * t)
self.axes.plot(t, s, color="red", linestyle="solid")


if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
ui = MainCanvas()
ui.show()
sys.exit(app.exec_())

main.py
from PyQt5.QtWidgets import *
from figure import MainCanvas


class RadioButton(QWidget):
def __init__(self):
super().__init__()

self.main_cavnas = MainCanvas()

self.InitUi()

def InitUi(self):
groups = {"Functions": ("Sinus", "Cosines"),
"Colors": ("Red", "Green"),
"Line Styles": ("Solid", "Dashed")
}

widget_layout = QVBoxLayout()

main_group = QGroupBox("Operations")
main_group_layout = QHBoxLayout()

for group, buttons in groups.items():
group_box = QGroupBox(group)
group_layout = QVBoxLayout()
for button_text in buttons:
button = QRadioButton(button_text)
button.setObjectName("radiobutton_%s" % button_text.lower())
if button_text == "Sinus" or button_text == "Red" or button_text == "Solid":
button.setChecked(True)

group_layout.addWidget(button)

group_box.setLayout(group_layout)
main_group_layout.addWidget(group_box)

main_group.setLayout(main_group_layout)
widget_layout.addWidget(self.main_cavnas)
widget_layout.addWidget(main_group)

self.setLayout(widget_layout)


if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
ui = RadioButton()
ui.show()
sys.exit(app.exec_())

最佳答案

逻辑是检测按钮状态的变化,然后获取信息,清理 Canvas 并重绘。

from PyQt5 import QtCore, QtWidgets

from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure

import numpy as np


class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super().__init__(parent)

fig = Figure(figsize=(5, 4), dpi=100)
self.canvas = FigureCanvas(fig)

groups = {
"Functions": (("Sinus", np.sin), ("Cosines", np.cos)),
"Colors": (("Red", "red"), ("Green", "green")),
"Line Styles": (("Solid", "solid"), ("Dashed", "dashed")),
}

main_group = QtWidgets.QGroupBox("Operations")
main_group_layout = QtWidgets.QHBoxLayout(main_group)

self.group_btns = []

for group_title, v in groups.items():
group = QtWidgets.QGroupBox(group_title)
main_group_layout.addWidget(group)
vlay = QtWidgets.QVBoxLayout(group)
group_btn = QtWidgets.QButtonGroup(self)
self.group_btns.append(group_btn)
for (text, value) in v:
radio = QtWidgets.QRadioButton(text)
radio.setProperty("prop", value)
vlay.addWidget(radio)
group_btn.addButton(radio)
if text in ("Sinus", "Red", "Solid"):
radio.setChecked(True)

for group_btn in self.group_btns:
group_btn.buttonToggled.connect(self.update_canvas)

lay = QtWidgets.QVBoxLayout(self)
lay.addWidget(self.canvas, stretch=1)
lay.addWidget(main_group)

self.update_canvas()

@QtCore.pyqtSlot()
def update_canvas(self):
func, color, style = (
group.checkedButton().property("prop") for group in self.group_btns
)
t = np.arange(0.0, 5.0, 0.01)
s = func(2 * np.pi * t)

self.canvas.figure.clear()
ax = self.canvas.figure.subplots()
ax.plot(t, s, color=color, linestyle=style)
self.canvas.draw()


if __name__ == "__main__":
import sys

app = QtWidgets.QApplication(sys.argv)
ui = Widget()
ui.show()
sys.exit(app.exec_())

关于python - 动态 Canvas 控制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62121654/

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