【问题标题】:Python - Adding a Tkinter Graph to a PyQt WidgetPython - 将 Tkinter 图形添加到 PyQt 小部件
【发布时间】:2011-12-13 22:30:18
【问题描述】:

我们目前有一个使用 PyQt 创建的功能齐全的 Gui。我的搭档在 Tkinter 中编写了一个绘制数据集的函数。我的问题是,我们如何将两者结合起来,使它们协同工作?

这是绘图功能:

def createGraph(self):
        import tkinter as tk

        # Send in data as param, OR
        #data = [17, 20, 15, 10, 7, 5, 4, 3, 2, 1, 1, 0]        

        # Recieve data within function 
        s.send("loadgraph")

        inputString = repr(s.recv(MSGSIZE))
        #inputString = "-20 15 10 7 5 -4 3 2 1 1 0"
        print(inputString)
        data = [int(x) for x in inputString.split()]

        root = tk.Tk()
        root.title("FSPwners")
        screen_width = 400
        screen_height = 700
        screen = tk.Canvas(root, width=screen_width, height=screen_height, bg= 'white')
        screen.pack()

        # highest y = max_data_value * y_stretch
        y_stretch = 15
        # gap between lower canvas edge and x axis
        y_gap = 350
        # stretch enough to get all data items in
        x_stretch = 10
        x_width = 20
        # gap between left canvas edge and y axis
        x_gap = 20


        for x, y in enumerate(data):
            # calculate reactangle coordinates (integers) for each bar
            x0 = x * x_stretch + x * x_width + x_gap
            y0 = screen_height - (y * y_stretch + y_gap)
            x1 = x * x_stretch + x * x_width + x_width + x_gap
            y1 = screen_height - y_gap
            # draw the bar
            print(x0, y0, x1, y1)
            if y < 0:
                screen.create_rectangle(x0, y0, x1, y1, fill="red")
            else:
                screen.create_rectangle(x0, y0, x1, y1, fill="green")

            # put the y value above each bar
            screen.create_text(x0+2, y0, anchor=tk.SW, text=str(y))

        root.mainloop()

当该方法自行运行时,它会创建一个带有图形的弹出框。现在我们希望它在我们当前的 gui 中按下按钮时创建一个弹出图。我们怎样才能让它发挥作用?如果我们在 GUI 中单击按钮时调用createGraph(),则会收到错误消息: 无法识别的选择器发送到实例 x009x...

有什么问题?谢谢!

【问题讨论】:

  • 那个函数看起来不太复杂。我认为用 PyQt 重写它比尝试集成 PyQt 和 Tk 更可取。

标签: python pyqt tkinter pyqt4


【解决方案1】:

Qt 和 Tkinter 不能很好地配合,正如您所看到的 - 我曾经玩过 Python 图形工具包,并写过 可以在 Qt、GTK 或 Tkinter 中运行的 4 运算计算器 - 甚至一次全部显示。

为了让 Tkinter 和 Qt 版本同时工作, 我不得不分叉这个过程 - 并以单独的方式启动每个工具包 运行实例;

您的情况并不相同,因为 Qt GUI 已经在运行,但也许 有了这个开始,你可以找到一个解决方法。

3-calculators 代码列表可以在这里找到:

http://www.python.org.br/wiki/CalculadoraTkGtkQt

【讨论】:

  • 谢谢!最终将 tkinter 放在一个单独的文件中,并使用 os.system("python graph.py") 调用它以使其工作
【解决方案2】:

这是一个 PyQt 端口:

from PyQt4 import QtCore, QtGui

class Graph(QtGui.QWidget):
    def __init__(self, data, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self._data = data
        self.resize(400, 700)
        self.setWindowTitle('FSPwners')
        self.setAutoFillBackground(True)
        self.setBackgroundRole(QtGui.QPalette.Base)

    def paintEvent(self, event):
        painter = QtGui.QPainter()
        painter.begin(self)

        screen_width = self.width()
        screen_height = self.height()

        # highest y = max_data_value * y_stretch
        y_stretch = 15
        # gap between lower canvas edge and x axis
        y_gap = 350
        # stretch enough to get all data items in
        x_stretch = 10
        x_width = 20
        # gap between left canvas edge and y axis
        x_gap = 20

        for x, y in enumerate(self._data):
            # calculate reactangle coordinates (integers) for each bar
            x0 = x * x_stretch + x * x_width + x_gap
            y0 = screen_height - (y * y_stretch + y_gap)
            x1 = x0 + x_width
            y1 = screen_height - y_gap
            if y < 0:
                painter.setBrush(QtCore.Qt.red)
            else:
                painter.setBrush(QtCore.Qt.green)
            painter.drawRect(QtCore.QRectF(
                QtCore.QPointF(x0, y0), QtCore.QPointF(x1, y1)))
            print (x0, y0, x1, y1)

            # put the y value above each bar
            painter.drawText(x0 + 2, y0 - 2, str(y))

        painter.end()

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    # data to be graphed
    data = [-20, 15, 10, 7, 5, -4, 3, 2, 1, 1, 0]
    window = Graph(data)
    window.show()
    sys.exit(app.exec_())

【讨论】:

    猜你喜欢
    • 2012-08-14
    • 2014-08-16
    • 1970-01-01
    • 2012-02-05
    • 2013-01-19
    • 1970-01-01
    • 1970-01-01
    • 2017-10-31
    • 2021-04-12
    相关资源
    最近更新 更多