我需要将一个comboBox连接到一个单独类的方法中定义的绘图函数,如下所示。所需的结果是在comboBox的项目中选择的范围是在图中绘制的范围,因此在选择时刷新画布也是必要的,我也可以使用建议。
我可以通过两种方式来实现这一结果,但两者都存在问题。理想情况下,comboBox将在包含绘图函数的类中定义,以避免从类外部将其连接到类(方法(函数)))的困难,但我不认为这是可能。或者,它可以在setupUI中定义,但是我如何将它连接到绘图函数?是否有可能从该类外部达到类的方法的功能?对这两种方法的建议将不胜感激。我尽可能地浓缩了代码。
import math
import matplotlib
import numpy as np
from PyQt4 import QtCore, QtGui
import matplotlib.pyplot as plt
from matplotlib.figure import Figure
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class MyMplCanvas(FigureCanvas):
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,
QtGui.QSizePolicy.Expanding,
QtGui.QSizePolicy.Expanding)
FigureCanvas.updateGeometry(self)
def compute_initial_figure(self):
pass
class UsersPerCountryAndPlatformBar(MyMplCanvas):
def compute_initial_figure(self):
A_B = [['Afghanistan', 'Albania', 'Algeria', 'American Samoa', 'Angola'], [1, 9, 7, 1, 1], [1, 7, 2, 0, 1], \
[0, 4, 3, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
C_F = [['Cambodia', 'Canada', 'Chile', 'China', 'Colombia'], [13, 189, 12, 5, 10], [5, 115, 6, 64, 7], \
[1, 11, 1, 2, 4], [2, 12, 0, 2, 0], [2, 5, 0, 0, 0]]
def plot(X_Y):
summedX_Y = []
for i in range(len(X_Y[0])):
summedX_Y.append(X_Y[1][i] + X_Y[2][i] + X_Y[3][i] + X_Y[4][i] + X_Y[5][i])
def roundup(x):
return int(math.ceil(x / 10.0)) * 10
N = len(X_Y[0])
ind = np.arange(N)
width = 0.75
colors = ['yellowgreen', 'paleturquoise', 'royalblue', 'lightsteelblue', 'firebrick']
print [x + y for x, y in zip(X_Y[1], X_Y[2])]
print [x + y + z for x, y, z in zip(X_Y[1], X_Y[2], X_Y[3])]
print [x + y + z + i for x, y, z, i in zip(X_Y[1], X_Y[2], X_Y[3], X_Y[4])]
p1 = self.axes.bar(ind, X_Y[1], width, color = colors[0])
p2 = self.axes.bar(ind, X_Y[2], width, bottom = X_Y[1], color = colors[1])
p3 = self.axes.bar(ind, X_Y[3], width, bottom = [x + y for x, y in zip(X_Y[1], X_Y[2])], color = colors[2])
p4 = self.axes.bar(ind, X_Y[4], width, bottom = [x + y + z for x, y, z in zip(X_Y[1], X_Y[2], X_Y[3])], color = colors[3])
p5 = self.axes.bar(ind, X_Y[5], width, bottom = [x + y + z + i for x, y, z, i in zip(X_Y[1], X_Y[2], X_Y[3], X_Y[4])], color = colors[4])
self.axes.set_ylabel('Count')
self.axes.set_title('Users Per Country And Platform')
self.axes.set_xticks(ind)
self.axes.set_xticklabels(countrylabels, fontsize = 8)
self.axes.set_yticklabels(np.arange(0, max(summedX_Y), roundup(max(summedX_Y) / 10)))
self.axes.legend((p1[0], p2[0], p3[0], p4[0], p5[0]), ('Android', 'iOS', 'Windows', 'OS X', 'WebGL'))
'''comboBox = QtGui.QComboBox(self)
comboBox.addItem('A_B')
comboBox.addItem('C_F')
comboBox.activated[str].connect(compute_initial_figure(plot(text)))'''
# should plot(A_B) or (C_F) as selected
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
self.main_widget = QtGui.QWidget(MainWindow)
l = QtGui.QVBoxLayout(self.main_widget)
UPCP = UsersPerCountryAndPlatformBar(self.main_widget, width = 10, height = 5, dpi = 100)
l.addWidget(UPCP)
self.main_widget.setFocus()
MainWindow.setCentralWidget(self.main_widget)
QtCore.QMetaObject.connectSlotsByName(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 :(得分:1)
您可以查看下面的代码。为了更好的可读性,我删除了双重子类。
初始化FigureCanvas时会调用 #define SYSTEM_VERSION_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)
组合位于主窗口类中,并连接到更新功能。此函数将组合框中的文本转换为传递给绘图函数的一些数据。
compute_initial_figure