首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Python -向PyQt小部件添加Tkinter Graph

Python -向PyQt小部件添加Tkinter Graph
EN

Stack Overflow用户
提问于 2011-12-14 06:30:19
回答 2查看 3.6K关注 0票数 5

我们目前有一个使用PyQt创建的全功能Gui。我的伙伴写了一个函数,用Tkinter绘制了一个dataSet。我的问题是,我们如何将两者结合起来,让它们协同工作?

下面是绘图函数:

代码语言:javascript
运行
复制
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中按下按钮时创建一个弹出图。我们怎么才能让它工作呢?如果我们只是在图形用户界面中单击一个按钮时调用createGraph(),我们会得到这样的错误:无法识别的选择器发送到实例x009x...

有什么问题吗?谢谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-12-14 06:53:48

Qt和Tkinter不能很好地配合,正如你所看到的-我曾经使用Python图形工具包,并编写了一个4次运算计算器,可以在Qt,GTK或Tkinter中运行-甚至可以一次显示所有内容。

为了同时运行Tkinter和Qt版本,我必须派生进程--并在单独的运行实例中启动每个工具包;

您的案例并不完全相同,因为Qt GUI已经在运行了,但是也许有了这样的启动,您就可以找到一种变通方法。

3个计算器的代码清单可以在这里找到:

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

票数 3
EN

Stack Overflow用户

发布于 2011-12-14 10:56:19

这是一个PyQt端口:

代码语言:javascript
运行
复制
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_())
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8497142

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档