2011-12-13 115 views
2

我們現在有一個使用PyQt創建的功能齊全的GUI。我的合作伙伴編寫了一個在Tkinter中繪製數據集的函數。我的問題是,我們如何將兩者結合起來,讓他們一起工作?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中按下按鈕時創建一個彈出圖。我們如何才能使它工作?如果我們只是在我們的GUI中點擊一個按鈕時調用createGraph(),我們得到錯誤: 發送到實例x009x的無法識別的選擇器...

問題是什麼?謝謝!

+1

這個功能看起來並不過於複雜。我認爲用PyQt重寫它會比嘗試整合PyQt和Tk更好。 – Avaris 2011-12-13 23:19:50

回答

2

Qt和Tkinter的不玩很合得來,你可以感覺到 - 我曾沿着Python的圖形工具包播放一次,並寫下 會在任何的Qt,GTK或Tkinter的運行4操作計算器 - 或甚至一次顯示。

爲了讓Tkinter和Qt版本同時工作,我必須分叉進程 - 並且在單獨的 運行實例中啓動每個工具包;

你的情況並不完全相同,因爲Qt GUI已經在運行,但也許有 有這個啓動,你可以伴隨一個解決方法。

三計算器代碼清單可以在這裏找到:

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

+0

謝謝!結束tkinter在一個單獨的文件中,並調用它`os.system(「python graph.py」)`使其工作 – CHawk 2011-12-14 02:40:28

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_())