2011-12-13 17 views
2

現在、PyQtを使って作成された、完全に機能するGuiがあります。私のパートナーは、TkinterのdataSetをグラフ化する関数を書きました。私の質問は、どのように2つを組み合わせて一緒に働くのでしょうか?ここでPython - PyQtウィジェットに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でボタンが押されたときにポップアップグラフを作成するようにします。どうすればそれを稼働させることができますか? 認識できないセレクタがインスタンスx009xに送信されました。

問題は何ですか?ありがとう!あなたが知覚することができるよう

+1

機能があまりにも複雑に見えることはありません。 PyQtとTkを統合しようとするよりも、PyQtを使って書き直す方がはるかに望ましいと思います。 – Avaris

答えて

2

QtとTkinterのは、非常によく一緒に演奏していない - 私はかつてのPythonグラフィカルツールキットに沿って演奏し、Qtの、GTKまたはTkinterのいずれかで実行します 4の操作電卓書いていた - かを一度にすべて表示することもできます。同時に作業TkinterのとQtの両方のバージョンを有するために

、 私はプロセスをフォークしなければならなかった - と別 実行中のインスタンス内の各ツールキットを起動します。

あなたのケースは同じではありません.Qt GUIは既に実行されていますが、 これを起動して回避することができます。

3電卓のコードリストは、ここで見つけることができます:

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

+0

ありがとう! tkinterを別のファイルに置き、 'os.system(" python graph.py ")でそれを呼び出して動作させる – CHawk

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_()) 
関連する問題