我是Python的新手,我想开发一个GUI程序。 我已经安装了pythonxy,我开发了一个QT4 GUI,我用pyuic4在py中进行了转换。
在我的程序中,当我点击按钮时,我想用新变量更新mplwidget。我想只更新小部件而不重启ui。如何修改以下解决方案以仅更新小部件。
非常感谢你的帮助和指示
from PyQt4 import QtCore, QtGui
from matplotlibwidget import MatplotlibWidget
import numpy as np
global y
global x
x=[1,2,3]
y=[1, 2, 1]
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.mplwidget = MatplotlibWidget(self.centralwidget)
self.mplwidget.setGeometry(QtCore.QRect(170, 150, 400, 300))
self.mplwidget.setObjectName("mplwidget")
self.mplwidget.setFocus()
self.mplwidget.axes.plot(x,y)
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.pushButton = QtGui.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(170, 60, 75, 23))
self.pushButton.setObjectName("pushButton")
QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL("clicked()"), self.plot)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def plot(self):
global y
global x
x = [2,3,4]
y = [2,2,1]
self.replot()
def replot(self):
Ui_MainWindow()
ui.setupUi(MainWindow)
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
答案 0 :(得分:0)
我不熟悉matplotlibwidget
,因此可能有更简单的方法来执行此操作,但您可以抓取Axes
对象并执行您想要的操作:
def replot_(self, x, y):
# plot the data and keep a reference to the `Line2D` object
ln, = self.matplotlibwidget.axes.plot(x, y)
# force the canvas to re-draw,
# matplotlibwidget may provide more direct access to the canvas
self.matplotlibwidget.axes.figure.canvas.draw()
return ln