2017-09-27 71 views
1

我有一個Pyqt4 pyuic生成的.py文件。在這個文件中,我有一個工具欄和一個連接到actionRotate動作的旋轉圖標。這是代碼的一小部分;從另一個文件訪問操作按鈕PyQt4

from PyQt4 import QtCore, QtGui 

try: 
    _fromUtf8 = QtCore.QString.fromUtf8 
except AttributeError: 
    def _fromUtf8(s): 
     return s 

try: 
    _encoding = QtGui.QApplication.UnicodeUTF8 
    def _translate(context, text, disambig): 
     return QtGui.QApplication.translate(context, text, disambig, _encoding) 
except AttributeError: 
    def _translate(context, text, disambig): 
     return QtGui.QApplication.translate(context, text, disambig) 

class Ui_Program(object): 
    def setupUi(self, UI_Class): 
     UI_Class.setObjectName(_fromUtf8("UI_Class")) 
       ... 
     self.toolBar = QtGui.QToolBar(UI_Class) 
     self.toolBar.setObjectName(_fromUtf8("toolBar")) 
     UI_Class.addToolBar(QtCore.Qt.TopToolBarArea, self.toolBar) 
     self.toolBar.addAction(self.actionRotate) 
     self.actionRotate = QtGui.QAction(UI_Class) 
     self.actionRotate.setCheckable(True) 
     self.actionRotate.setIcon(icon10) 
     self.actionRotate.setObjectName(_fromUtf8("actionRotate")) 

所以,如果我嘗試它是否被選中或不從它的工作原理是在底部例如另一個類到達actionRotate按鈕;

import PyQt4 
import sys 
from PyQt4.QtGui import * 
from PyQt4 import QtGui, QtCore 
from PyQt4 import QtGui 
from DropDownActions import * 
import pickle 
import OpenGLcode 
from OpenGL.GL import * 
import PYQT_PROGRAM 
import numpy as np 
import sqlite3 as sq 

try: 
    _fromUtf8 = QtCore.QString.fromUtf8 
except AttributeError: 
    def _fromUtf8(s): 
     return s 

try: 
    from OpenGL import GL 
except ImportError: 
    app = QtGui.QApplication(sys.argv) 
    QtGui.QMessageBox.critical(None, "OpenGL hellogl", 
      "PyOpenGL must be installed to run this example.") 
    sys.exit(1) 

class UI_main_subclass(QMainWindow): 

    def __init__(self, ui_layout): 

     QMainWindow.__init__(self) 
     self.ui = ui_layout 
     ui_layout.setupUi(self) 
     .... 
     var1 = ui_layout.actionRotate.isChecked() 

但是當我嘗試從我的OpenGL代碼到達動作時,我無法做到這一點。以下代碼顯示相關部分;

from OpenGL.GL import * 
from PyQt4.QtOpenGL import * 
from PyQt4 import QtCore 

class glWidget(QGLWidget, QMainWindow): 
    resized = QtCore.pyqtSignal() 

    xRotationChanged = QtCore.pyqtSignal(int) 
    yRotationChanged = QtCore.pyqtSignal(int) 
    zRotationChanged = QtCore.pyqtSignal(int) 

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

    def mousePressEvent(self, event): 
     if self.ui.actionRotate.isChecked(self): # this code gives error below 
      print("test 1") 
      x, y = event.x(), event.y() 
      w, h = self.width(), self.height() 
      # required to call this to force PyQt to read from the correct, updated buffer 
      glReadBuffer(GL_FRONT) 
      data = self.grabFrameBuffer() # builtin function that calls glReadPixels internally 
      rgba = QColor(data.pixel(x, y)).getRgb() # gets the appropriate pixel data as an RGBA tuple 
      message = "You selected pixel ({0}, {1}) with an RGBA value of {2}.".format(x, y, rgba) 

      self.lastPos = event.pos() 

     else: 
      pass 

錯誤是;

AttributeError: 'glWidget' object has no attribute 'ui' 

我不知道爲什麼這不允許我達到主要的.py文件中生成的動作按鈕,從pyuic?

任何幫助表示讚賞...

+0

m包含'glWidget'類的模塊甚至不會導入'Ui_Program'或'UI_main_subclass',更不用說試圖以任何方式使用它們。所以我不明白你爲什麼認爲它應該起作用。 – ekhumoro

+0

我實際上導入了 'import UI_Program' ,我無法導入'UI_main_subclass',因爲'glWidget'類文件在'UI_main_subclass'中很重要。 – BjkOcean

+0

但是'glWidget'不會繼承'UI_Program',也不會創建它的一個實例。那麼你爲什麼認爲它應該有一個'ui'屬性呢? – ekhumoro

回答

1

看來你有大部分都安裝好了的:你只需要正確地使用它們。

在主窗口類,通過ui_layoutglWidget

class SABRE2_main_subclass(QMainWindow):  
    def __init__(self, ui_layout): 
     QMainWindow.__init__(self) 
     self.ui = ui_layout 
     ... 
     self.OpenGLwidget = OpenGLcode.glWidget(ui_layout) 

參考然後保持到它裏面的glWidget

class glWidget(QGLWidget, QMainWindow):  
    def __init__(self, ui_layout, parent = None): 
     super(glWidget,self).__init__(parent) 
     self.ui = ui_layout 

現在glWidget可以訪問的ui_layout屬性:

def mousePressEvent(self, event): 
     if self.ui.actionRotate.isChecked(): 
      print("test 1") 
+0

謝謝,這解決了問題與行動按鈕,但它會拋出箭頭與' DropDownActions.statusMessage(self.ui,message)'在'glWidget'的第86行中。我該如何解決這個問題? – BjkOcean

+0

你必須在'glWidget'中的每個地方使用'self.ui',而不是'self.ui_layout'(請參閱我的答案中的修改代碼)。那麼你應該可以執行'DropDownActions.statusMessage(self,message)'(因爲'self'現在具有'ui'屬性)。 (PS:如果你發現這個答案有用,請upvote/accept)。 – ekhumoro

+0

非常感謝,效果很好 – BjkOcean