2017-05-04 52 views
1

下降建模工具,我不知道怎麼問這一點,但在這裏不用....創建一拖在Python

我想創建一個定義約10個不同的對象的用戶界面。

這些目的都可以彼此連接。

是否有基於Python的工具,可以很容易地定義一個簡單的用戶界面和一些可拖動對象的畫布。

例如,可以說我有一個python框架來解決powerflow。我以編程方式創建網絡。

現在我只想定義一些對象,每個對象都有一個小圖片,將它們拖到畫布上,右鍵單擊它們以設置它們的設置。

我可以調查所有正常的Python的東西,但只是檢查是否可能不是一個工具,可以幫助自動化。

我添加了一個商業工具的圖片。 託比

enter image description here

+0

PyQt的或PySide可以幫助你。使用PIP安裝PySide安裝PySide。轉到PySide安裝文件夾「C:\ Python27 \ LIB \站點包\ PySide \實例」會通過各種例子去。例子可能會幫助你完成你的任務。 –

+0

您好,感謝您的回答,但我認爲這正是我不問。我理解這些框架,並在之前使用過它們。我的問題是更關係到「是否有一個框架,使構建一個簡單的建模工具,具有拖放界面簡單。 – Tooblippe

+0

對不起,我無法正確地理解你的問題,你能不能給我工具的任何例子你可能會尋找相似的。我使用的具有拖放界面Qt設計建造的形式,我想大家都知道Qt設計的。 –

回答

2

是否必須是一個原生UI?如果沒有,我會建議使用類似D3.js的可視化,python作爲後端和AJAX進行通信。這比使用PyQt或PyTk實現類似的功能要高效得多。

至於一些現成的包裝箱,我不知道是否有任何。

+0

中添加了商業工具的屏幕截圖這是一個很好的觀點。我認爲如果它啓用了網絡,它甚至會更好。 – Tooblippe

1

要實現您的任務,您需要將qtnodes(https://github.com/cb109/qtnodes)模塊與Pyqt拖放代碼結合使用。以下代碼可能對您有所幫助,但您需要閱讀qtnodes的內部代碼。如果您可以妥協拖放選項,那麼僅使用qtnodes構建應用程序將很容易。

您可以構建與Tobie相同的工具,但您需要使用pyside或pyqt或tkinter從頭開始編寫所有代碼。

拖放列表以圖形場景

from PyQt4 import QtCore, QtGui 
import sys 

class GraphicsScene(QtGui.QGraphicsScene): 

    def __init__(self, parent = None): 
     super(GraphicsScene, self).__init__(parent) 

    def dragEnterEvent(self, event): 
     event.accept() 

    def dragMoveEvent(self, event): 
     event.accept() 

    def dragLeaveEvent(self, event): 
     event.accept() 

    def dropEvent(self, event): 
     text = QtGui.QGraphicsTextItem(event.mimeData().text()) 
     text.setPos(event.scenePos()) 
     self.addItem(text) 
     event.accept() 

class ListView(QtGui.QListView): 

    def __init__(self, parent = None): 
     super(ListView, self).__init__(parent) 
     self.setDragEnabled(True) 

    def dragEnterEvent(self, event): 
     event.setDropAction(QtCore.Qt.MoveAction) 
     event.accept() 

    def startDrag(self, event): 
     index = self.indexAt(event.pos()) 
     if not index.isValid(): 
      return 

     selected = self.model().data(index, QtCore.Qt.DisplayRole) 

     mimeData = QtCore.QMimeData() 
     mimeData.setText(selected.toString()) 

     drag = QtGui.QDrag(self) 
     drag.setMimeData(mimeData) 

     result = drag.start(QtCore.Qt.MoveAction) 
     if result: # == QtCore.Qt.MoveAction: 
      pass 

    def mouseMoveEvent(self, event): 
     self.startDrag(event) 

class MainWindow(QtGui.QMainWindow): 

    def __init__(self, parent = None): 
     super(MainWindow, self).__init__(parent) 

     self.setGeometry(100, 100, 400, 400) 

     self.widget = QtGui.QWidget() 
     self.setCentralWidget(self.widget) 
     layout = QtGui.QGridLayout(self.widget) 

     self.ListView = ListView() 

     data = QtCore.QStringList() 
     data << "one" << "two" << "three" 
     self.model = QtGui.QStringListModel(data) 


     self.ListView.setModel(self.model) 

     self.GraphicsView = QtGui.QGraphicsView() 
     self.scene = GraphicsScene() 
     self.GraphicsView.setScene(self.scene) 
     #self.GraphicsView.setSceneRect(0, 0, self.GraphicsView.width(), self.GraphicsView.height()) 

     layout.addWidget(self.ListView, 0, 0, 5, 5) 
     layout.addWidget(self.GraphicsView, 0, 1, 5, 5) 

     self.show() 
     self.GraphicsView.setSceneRect(0, 0, self.GraphicsView.width(), self.GraphicsView.height()) 

if __name__ == "__main__": 
    app = QtGui.QApplication(sys.argv) 
    window = MainWindow() 
    sys.exit(app.exec_()) 

下面代碼是拖動和按鈕

from PyQt4 import QtGui, QtCore 

class DragButton(QtGui.QPushButton): 

    def __init__(self, parent): 
     super(DragButton, self).__init__(parent) 
     self.allowDrag = True 

    def setAllowDrag(self, allowDrag): 
     if type(allowDrag) == bool: 
      self.allowDrag = allowDrag 
     else: 
      raise TypeError("You have to set a boolean type") 

    def mouseMoveEvent(self, e): 
     if e.buttons() != QtCore.Qt.RightButton: 
      return 

     if self.allowDrag == True: 
      # write the relative cursor position to mime data 
      mimeData = QtCore.QMimeData() 
      # simple string with 'x,y' 
      mimeData.setText('%d,%d' % (e.x(), e.y())) 
      print mimeData.text() 

      # let's make it fancy. we'll show a "ghost" of the button as we drag 
      # grab the button to a pixmap 
      pixmap = QtGui.QPixmap.grabWidget(self) 

      # below makes the pixmap half transparent 
      painter = QtGui.QPainter(pixmap) 
      painter.setCompositionMode(painter.CompositionMode_DestinationIn) 
      painter.fillRect(pixmap.rect(), QtGui.QColor(0, 0, 0, 127)) 
      painter.end() 

      # make a QDrag 
      drag = QtGui.QDrag(self) 
      # put our MimeData 
      drag.setMimeData(mimeData) 
      # set its Pixmap 
      drag.setPixmap(pixmap) 
      # shift the Pixmap so that it coincides with the cursor position 
      drag.setHotSpot(e.pos()) 

      # start the drag operation 
      # exec_ will return the accepted action from dropEvent 
      if drag.exec_(QtCore.Qt.LinkAction | QtCore.Qt.MoveAction) == QtCore.Qt.LinkAction: 
       print 'linked' 
      else: 
       print 'moved' 

    def mousePressEvent(self, e): 
     QtGui.QPushButton.mousePressEvent(self, e) 
     if e.button() == QtCore.Qt.LeftButton: 
      print 'press' 
      #AQUI DEBO IMPLEMENTAR EL MENU CONTEXTUAL 

    def dragEnterEvent(self, e): 
     e.accept() 

    def dropEvent(self, e): 
     # get the relative position from the mime data 
     mime = e.mimeData().text() 
     x, y = map(int, mime.split(',')) 

      # move 
      # so move the dragged button (i.e. event.source()) 
     print e.pos() 
      #e.source().move(e.pos()-QtCore.QPoint(x, y)) 
      # set the drop action as LinkAction 
     e.setDropAction(QtCore.Qt.LinkAction) 
     # tell the QDrag we accepted it 
     e.accept() 

所有我已經從堆棧溢出複製的代碼的下降。通過下面的鏈接瞭解更多詳情。

Drag n Drop inside QgraphicsView doesn't work (PyQt)

Drag n Drop Button and Drop-down menu PyQt/Qt designer