Problem loading VTK window into PyQT Window - python

I'm tearing my hair out with a problem with VTK and PyQt4. I have an interface that has two tabs in it, one will house the VTK window and the other graphs.
The code for ui_Test which sets up the interface is as follows:
# -*- coding: utf-8 -*-
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_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(800, 600)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.gridLayout = QtGui.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
self.verticalLayout = QtGui.QVBoxLayout()
self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
self.tabWidget = QtGui.QTabWidget(self.centralwidget)
self.tabWidget.setObjectName(_fromUtf8("tabWidget"))
self.tab = QtGui.QWidget()
self.tab.setObjectName(_fromUtf8("tab"))
self.gridLayout_2 = QtGui.QGridLayout(self.tab)
self.gridLayout_2.setObjectName(_fromUtf8("gridLayout_2"))
self.vtkLayout = QtGui.QVBoxLayout()
self.vtkLayout.setObjectName(_fromUtf8("vtkLayout"))
self.gridLayout_2.addLayout(self.vtkLayout, 0, 0, 1, 1)
self.tabWidget.addTab(self.tab, _fromUtf8(""))
self.tab_2 = QtGui.QWidget()
self.tab_2.setObjectName(_fromUtf8("tab_2"))
self.tabWidget.addTab(self.tab_2, _fromUtf8(""))
self.verticalLayout.addWidget(self.tabWidget)
self.gridLayout.addLayout(self.verticalLayout, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 31))
self.menubar.setObjectName(_fromUtf8("menubar"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
self.tabWidget.setCurrentIndex(0)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("MainWindow", "Tab 1", None))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("MainWindow", "Tab 2", None))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
And the main window code is:
#!/usr/bin/env python
import sys
from vtk import vtkSphereSource,vtkRenderer,vtkColorTransferFunction,vtkPoints,vtkCellArray,vtkUnsignedCharArray,vtkPolygon,vtkPolyData,VTK_MAJOR_VERSION,vtkPolyDataMapper,vtkActor,vtkProperty,vtkScalarBarActor
from PyQt4 import QtCore, QtGui
from vtk.qt4.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
import ui_Test
class MainWindow(QtGui.QMainWindow,ui_Test.Ui_MainWindow):
def __init__(self, parent = None):
QtGui.QMainWindow.__init__(self, parent)
self.setupUi(self)
self.vtkWidget = QVTKRenderWindowInteractor(self)
self.vtkLayout.addWidget(self.vtkWidget)
self.ren = vtkRenderer()
self.vtkWidget.GetRenderWindow().AddRenderer(self.ren)
self.iren = self.vtkWidget.GetRenderWindow().GetInteractor()
# Create source
source = vtkSphereSource()
source.SetCenter(0, 0, 0)
source.SetRadius(5.0)
# Create a mapper
mapper = vtkPolyDataMapper()
mapper.SetInputConnection(source.GetOutputPort())
# Create an actor
actor = vtkActor()
actor.SetMapper(mapper)
self.ren.AddActor(actor)
self.ren.ResetCamera()
self.iren.Initialize()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
The problem is that when this runs it throughs errors in the VTK error log window and nothing shows on the tab. The error looks something like this:
ERROR: In C:\VPP\standalone-build\VTK-source\Rendering\OpenGL2\vtkShaderProgram.cxx, line 431
vtkShaderProgram (0000016142650060):
ERROR: In C:\VPP\standalone-build\VTK-source\Rendering\OpenGL2\vtkWin32OpenGLRenderWindow.cxx, line 769
vtkWin32OpenGLRenderWindow (00000161425DD820): failed to get valid pixel format.
ERROR: In C:\VPP\standalone-build\VTK-source\Rendering\OpenGL2\vtkOpenGLRenderWindow.cxx, line 785
vtkWin32OpenGLRenderWindow (00000161425DD820): GLEW could not be initialized.
ERROR: In C:\VPP\standalone-build\VTK-source\Rendering\OpenGL2\vtkShaderProgram.cxx, line 430
vtkShaderProgram (0000016142650060): 1: #version 120
I won't post the full thing as it is really long! I can make the PyQT VTK examples run so the VTK/PyQT version isn't the problem - I assume it is to do with how I'm adding it to the tab. Any help will be massively appreciated!

Sorry, I'm using PyQt5, everything works without problems.
main.py
import sys
#from PyQt4 import QtCore, QtGui
from PyQt5 import QtCore, QtGui, QtWidgets
from vtk import vtkSphereSource,vtkRenderer,vtkColorTransferFunction,vtkPoints,vtkCellArray,vtkUnsignedCharArray,vtkPolygon,vtkPolyData,VTK_MAJOR_VERSION,vtkPolyDataMapper,vtkActor,vtkProperty,vtkScalarBarActor
#from vtk.qt4.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
from vtk.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
import ui_Test
class MainWindow(QtWidgets.QMainWindow, ui_Test.Ui_MainWindow):
def __init__(self, parent = None):
QtWidgets.QMainWindow.__init__(self, parent)
self.setupUi(self)
self.vtkWidget = QVTKRenderWindowInteractor(self)
self.vtkLayout.addWidget(self.vtkWidget)
self.ren = vtkRenderer()
self.vtkWidget.GetRenderWindow().AddRenderer(self.ren)
self.iren = self.vtkWidget.GetRenderWindow().GetInteractor()
# Create source
source = vtkSphereSource()
source.SetCenter(0, 0, 0)
source.SetRadius(5.0)
# Create a mapper
mapper = vtkPolyDataMapper()
mapper.SetInputConnection(source.GetOutputPort())
# Create an actor
actor = vtkActor()
actor.SetMapper(mapper)
self.ren.AddActor(actor)
self.ren.ResetCamera()
self.iren.Initialize()
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
ui_Test.py
#from PyQt4 import QtCore, QtGui
from PyQt5 import QtCore, QtGui, QtWidgets
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtWidgets.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtWidgets.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtWidgets.QApplication.translate(context, text, disambig)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
self.verticalLayout = QtWidgets.QVBoxLayout()
self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
self.tabWidget = QtWidgets.QTabWidget(self.centralwidget)
self.tabWidget.setObjectName(_fromUtf8("tabWidget"))
self.tab = QtWidgets.QWidget()
self.tab.setObjectName(_fromUtf8("tab"))
self.gridLayout_2 = QtWidgets.QGridLayout(self.tab)
self.gridLayout_2.setObjectName(_fromUtf8("gridLayout_2"))
self.vtkLayout = QtWidgets.QVBoxLayout()
self.vtkLayout.setObjectName(_fromUtf8("vtkLayout"))
self.gridLayout_2.addLayout(self.vtkLayout, 0, 0, 1, 1)
self.tabWidget.addTab(self.tab, _fromUtf8(""))
self.tab_2 = QtWidgets.QWidget()
self.tab_2.setObjectName(_fromUtf8("tab_2"))
self.tabWidget.addTab(self.tab_2, _fromUtf8(""))
self.verticalLayout.addWidget(self.tabWidget)
self.gridLayout.addLayout(self.verticalLayout, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 31))
self.menubar.setObjectName(_fromUtf8("menubar"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
self.tabWidget.setCurrentIndex(0)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("MainWindow", "Tab 1", None))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("MainWindow", "Tab 2", None))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())

Related

issue with ScrollArea in Pyqt using QtDesigner

I designed a window with QtDesigner and added a scrollarea and a layout in it, but the scroll area doesn't scroll, it shows the scroll bar but It cant be scrolled at all.
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 StartQT4(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(767, 600)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.pushButton = QtGui.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(230, 300, 75, 23))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.lineEdit = QtGui.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(80, 330, 371, 71))
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 767, 21))
self.menubar.setObjectName(_fromUtf8("menubar"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
self.scrollArea = QtGui.QScrollArea(self.centralwidget)
self.scrollArea.setGeometry(QtCore.QRect(130, 70, 301, 191))
self.scrollArea.setWidgetResizable(True)
self.scrollArea.setMinimumSize(QtCore.QSize(100, 100))
self.scrollArea.setObjectName(_fromUtf8("scrollArea"))
self.scrollAreaWidgetContents = QtGui.QWidget()
self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 299, 189))
self.scrollAreaWidgetContents.setObjectName(_fromUtf8("scrollAreaWidgetContents"))
self.scrollAreaWidgetContents.setMinimumSize(QtCore.QSize(100, 100))
self.scrollArea.setWidget(self.scrollAreaWidgetContents)
self.scrollArea.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
self.scrollArea.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.horizontalLayoutWidget = QtGui.QWidget(self.scrollAreaWidgetContents)
self.horizontalLayoutWidget.setGeometry(QtCore.QRect(-1, -1, 301, 191))
self.horizontalLayoutWidget.setObjectName(_fromUtf8("horizontalLayoutWidget"))
self.horizontalLayout = QtGui.QVBoxLayout(self.horizontalLayoutWidget)
self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
p= QtGui.QPalette()
p.setColor(QtGui.QPalette.Background, QtCore.Qt.white)
self.scrollArea.setAutoFillBackground(True)
self.scrollArea.setPalette(p)
self.retranslateUi(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.pushButton.setText(_translate("MainWindow", "send", None))
I want to add labels and this is how I do it
from main_ui import *
import sys
global height
height=10
def send():
global height
label=QtGui.QLabel(myapp.ui.horizontalLayoutWidget)
label.setGeometry(QtCore.QRect(10, height, 46, 13))
height=height+label.height()
label.setText(myapp.ui.lineEdit.text())
label.show()
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
app = QtGui.QApplication(sys.argv)
myapp = StartQT4()
myapp.show()
QtCore.QObject.connect(myapp.ui.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), send)
myapp.setCentralWidget(myapp.ui.centralwidget)
QtCore.QMetaObject.connectSlotsByName(myapp)
sys.exit(app.exec_())
Sir, your code is a bit messed.
Here is a small example, see if it works. There is no secret. :D
it's in PyQt5. You just gotta change your imports to PyQt4.
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QScrollArea
from PyQt5.QtWidgets import QVBoxLayout
from PyQt5.QtWidgets import QWidget
class WSlideBar(QWidget):
"""WSlideBar is a personalized slide bar."""
w_container = None
v_layout_container = None
v_scroll_area = None
v_layout_preview = None
def __init__(self):
"""Init UI."""
super(WSlideBar, self).__init__()
self.init_ui()
def init_ui(self):
"""Init all ui object requirements."""
self.setFixedSize(100,500)
self.setStyleSheet("""
background: gray;
""")
# Container Widget
self.w_container = QWidget()
self.w_container.setFixedWidth(100)
# Layout of Container Widget
self.v_layout_container = QVBoxLayout()
self.v_layout_container.setSpacing(100)
aux = QWidget()
aux.setFixedSize(10,10)
aux.setStyleSheet("""background: red;""")
aux2 = QWidget()
aux2.setFixedSize(20, 20)
aux2.setStyleSheet("""background: blue;""")
aux3 = QWidget()
aux3.setFixedSize(15, 15)
aux3.setStyleSheet("""background: yellow;""")
aux4 = QWidget()
aux4.setFixedSize(50,50)
aux4.setStyleSheet("""background: rgb(0,255,0,30%);""")
aux5 = QWidget()
aux5.setFixedSize(40, 40)
aux5.setStyleSheet("""background: green;""")
aux6 = QWidget()
aux6.setFixedSize(40, 40)
aux6.setStyleSheet("""background: green;""")
self.v_layout_container.addWidget(aux)
self.v_layout_container.addWidget(aux2)
self.v_layout_container.addWidget(aux3)
self.v_layout_container.addWidget(aux4)
self.v_layout_container.addWidget(aux5)
self.v_layout_container.addWidget(aux6)
self.w_container.setLayout(self.v_layout_container)
self.v_scroll_area = QScrollArea(self)
self.v_scroll_area.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
self.v_scroll_area.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.v_scroll_area.setWidget(self.w_container)
# Scroll Area Layer add
self.v_layout_preview = QVBoxLayout()
self.setLayout(self.v_layout_preview)
self.v_layout_preview.addWidget(self.v_scroll_area)
def run():
app = QApplication(sys.argv)
GUI = WSlideBar()
GUI.show()
sys.exit(app.exec_())
run()
OBS: You will just have to change for QPushButtons, QLabels,... instead of small QWidgets and whatever you need.

AttributeError: 'dataAcquisition' object has no attribute 'gpioButton'

I'm trying to connect a push-button on my main window to open a new widget when I click it. But each time I run the program I get this error:
AttributeError: 'dataAcquisition' object has no attribute 'gpioButton'
I will be grateful if someone can help.
Here's my code:
from PyQt4 import QtCore, QtGui
from mainwindow import Ui_MainWindow
from gpiodialog import Ui_GPIODialog
import sys
class gpioDialog(QtGui.QDialog, Ui_GPIODialog):
def __init__(self, parent=None):
QtGui.QDialog.__init__(self, parent)
flags = QtCore.Qt.Drawer | QtCore.Qt.WindowStaysOnTopHint
self.setWidowFlags(flags)
self.ui = Ui_GPIODialog
self.ui.setupUi(self)
self.gpioOKButton.clicked.connect(self.acceptOKButtonClicked)
def acceptOKButtonClicked(self):
self.close()
class dataAcquisition(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.gpioButton.clicked.connect(self.gpioButton_clicked)
self.popGPIO = gpioDialog()
def gpioButton_clicked(self):
self.popGPIO.show()
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
MainWindow = dataAcquisition()
MainWindow.show()
sys.exit(app.exec_())
mainwindow.py code:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'mainwindow.ui'
#
# Created: Tue Oct 18 02:57:57 2016
# by: PyQt4 UI code generator 4.11.2
#
# WARNING! All changes made in this file will be lost!
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_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(700, 600)
MainWindow.setMinimumSize(QtCore.QSize(700, 600))
MainWindow.setMaximumSize(QtCore.QSize(700, 600))
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.gpioButton = QtGui.QToolButton(self.centralwidget)
self.gpioButton.setGeometry(QtCore.QRect(20, 80, 181, 61))
self.gpioButton.setObjectName(_fromUtf8("gpioButton"))
self.procButton = QtGui.QPushButton(self.centralwidget)
self.procButton.setGeometry(QtCore.QRect(20, 180, 181, 61))
self.procButton.setObjectName(_fromUtf8("procButton"))
self.startButton = QtGui.QPushButton(self.centralwidget)
self.startButton.setEnabled(True)
self.startButton.setGeometry(QtCore.QRect(20, 360, 131, 51))
self.startButton.setObjectName(_fromUtf8("startButton"))
self.stopButton = QtGui.QPushButton(self.centralwidget)
self.stopButton.setGeometry(QtCore.QRect(190, 360, 131, 51))
self.stopButton.setObjectName(_fromUtf8("stopButton"))
self.plotButton = QtGui.QPushButton(self.centralwidget)
self.plotButton.setGeometry(QtCore.QRect(20, 460, 181, 61))
self.plotButton.setObjectName(_fromUtf8("plotButton"))
self.columnView = QtGui.QColumnView(self.centralwidget)
self.columnView.setGeometry(QtCore.QRect(340, 50, 341, 251))
self.columnView.setObjectName(_fromUtf8("columnView"))
self.label_2 = QtGui.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(280, 20, 221, 21))
font = QtGui.QFont()
font.setPointSize(15)
font.setBold(True)
font.setWeight(75)
self.label_2.setFont(font)
self.label_2.setObjectName(_fromUtf8("label_2"))
self.saveButton = QtGui.QPushButton(self.centralwidget)
self.saveButton.setEnabled(True)
self.saveButton.setGeometry(QtCore.QRect(410, 420, 211, 31))
self.saveButton.setObjectName(_fromUtf8("saveButton"))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 700, 27))
self.menubar.setObjectName(_fromUtf8("menubar"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.gpioButton.setText(_translate("MainWindow", "GPIO Number of inputs", None))
self.procButton.setText(_translate("MainWindow", "Processing", None))
self.startButton.setText(_translate("MainWindow", "Start Acquiring", None))
self.stopButton.setText(_translate("MainWindow", "Stop Acquiring", None))
self.plotButton.setText(_translate("MainWindow", "Start Live Plot", None))
self.label_2.setText(_translate("MainWindow", "Data Acquisition System", None))
self.saveButton.setText(_translate("MainWindow", "Save this session\'s data", None))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
gpiodialog.py code:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'gpiodialog.ui'
#
# Created: Tue Oct 18 03:11:51 2016
# by: PyQt4 UI code generator 4.11.2
#
# WARNING! All changes made in this file will be lost!
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_GPIODialog(object):
def setupUi(self, GPIODialog):
GPIODialog.setObjectName(_fromUtf8("GPIODialog"))
GPIODialog.resize(400, 300)
self.gpioOKButton = QtGui.QPushButton(GPIODialog)
self.gpioOKButton.setGeometry(QtCore.QRect(170, 240, 101, 31))
self.gpioOKButton.setObjectName(_fromUtf8("gpioOKButton"))
self.label = QtGui.QLabel(GPIODialog)
self.label.setGeometry(QtCore.QRect(90, 20, 191, 21))
font = QtGui.QFont()
font.setPointSize(13)
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setObjectName(_fromUtf8("label"))
self.retranslateUi(GPIODialog)
QtCore.QMetaObject.connectSlotsByName(GPIODialog)
def retranslateUi(self, GPIODialog):
GPIODialog.setWindowTitle(_translate("GPIODialog", "Dialog", None))
self.gpioOKButton.setText(_translate("GPIODialog", "OK", None))
self.label.setText(_translate("GPIODialog", "Choose GPIO input bins", None))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
GPIODialog = QtGui.QDialog()
ui = Ui_GPIODialog()
ui.setupUi(GPIODialog)
GPIODialog.show()
sys.exit(app.exec_())
You created a separate namespace for the ui, so you would need to access the button like this:
self.ui.gpioButton.clicked.connect(self.gpioButton_clicked)
However, if you'd prefer to have all the widgets as attributes of the window, do this:
class gpioDialog(QtGui.QDialog, Ui_GPIODialog):
def __init__(self, parent=None):
super(gpioDialog, self).__init__(parent)
flags = QtCore.Qt.Drawer | QtCore.Qt.WindowStaysOnTopHint
self.setWindowFlags(flags)
self.setupUi(self)
self.gpioOKButton.clicked.connect(self.acceptOKButtonClicked)
class dataAcquisition(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(dataAcquisition, self).__init__(parent)
self.setupUi(self)
self.gpioButton.clicked.connect(self.gpioButton_clicked)
self.popGPIO = gpioDialog()

Qt/PyQt GraphicView

I am trying to recreate some pyqt codes using qt designer.I generated a simple python code using pyuic4.The image should load only in the QgraphicView area but it covers the entire window and I could not see the push buttons.
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_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(608, 526)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.widget = QtGui.QWidget(self.centralwidget)
self.widget.setGeometry(QtCore.QRect(300, 20, 258, 244))
self.widget.setObjectName(_fromUtf8("widget"))
self.verticalLayout = QtGui.QVBoxLayout(self.widget)
self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
self.graphicsView = QtGui.QGraphicsView(self.widget)
self.graphicsView.setEnabled(True)
self.graphicsView.setMinimumSize(QtCore.QSize(200, 200))
self.graphicsView.setMaximumSize(QtCore.QSize(400, 400))
self.graphicsView.setObjectName(_fromUtf8("graphicsView"))
self.verticalLayout.addWidget(self.graphicsView)
self.horizontalLayout = QtGui.QHBoxLayout()
self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
self.pushButton = QtGui.QPushButton(self.widget)
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.horizontalLayout.addWidget(self.pushButton)
self.pushButton_2 = QtGui.QPushButton(self.widget)
self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
self.horizontalLayout.addWidget(self.pushButton_2)
self.verticalLayout.addLayout(self.horizontalLayout)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 608, 22))
self.menubar.setObjectName(_fromUtf8("menubar"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.pushButton.setText(_translate("MainWindow", "PushButton", None))
self.pushButton_2.setText(_translate("MainWindow", "PushButton", None))
and the main file is :
import sys
from PyQt4 import QtCore, QtGui
from graphics_test_output import Ui_MainWindow
class Main(QtGui.QMainWindow,Ui_MainWindow):
def __init__(self):
super(Main,self).__init__()
self.setupUi(self)
self.mw = MainWidget()
self.setCentralWidget(self.mw)
class MainWidget(QtGui.QWidget):
def __init__(self, parent=None):
super(MainWidget,self).__init__(parent)
self.scene = QtGui.QGraphicsScene()
self.view = QtGui.QGraphicsView(self.scene)
layout = QtGui.QVBoxLayout()
layout.addWidget(self.view)
self.setLayout(layout)
self.pixmap_item = QtGui.QGraphicsPixmapItem(QtGui.QPixmap('Image.png'), None, self.scene)
if __name__=='__main__':
app = QtGui.QApplication(sys.argv)
window = Main()
window.show()
sys.exit(app.exec_())
I have added screenshots of ui design and the current output and they would appear.
ui_design:
current_output:

how to create label button in PYQT?

I need your help with my app. I undecorated python gui and now i need to create clicable label.
run.py
from PyQt4 import QtCore, QtGui
import sys
import untitled
class ExampleApp(QtGui.QMainWindow, untitled.Ui_MainWindow):
def __init__(self):
super(self.__class__, self).__init__()
self.setupUi(self)
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
def main():
app = QtGui.QApplication(sys.argv)
form = ExampleApp()
form.show()
app.exec_()
if __name__ == '__main__':
main()
def closeapp():
sys.exit(0);
untitled.py:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!
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_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(800, 600)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.pushButton = QtGui.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(40, 110, 85, 30))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.label = QtGui.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(220, 100, 58, 14))
self.label.setObjectName(_fromUtf8("label"))
self.label_2 = QtGui.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(220, 140, 58, 14))
self.label_2.setObjectName(_fromUtf8("label_2"))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26))
self.menubar.setObjectName(_fromUtf8("menubar"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.pushButton.setText(_translate("MainWindow", "open", None))
self.label.setText(_translate("MainWindow", "close", None))
self.label_2.setText(_translate("MainWindow", "view", None))
Now I need to add action to label and label_2. How to make the label to perform closeapp()?
Add a function to Ui_MainWindow class (untitled.py)
def click(self,eve):
print "clicked"
Then add attribute to label object
self.label.mousePressEvent = self.click
another option is just modify run.py file like following
class ExampleApp(QtGui.QMainWindow, untitled.Ui_MainWindow):
def click(self,eve):
print "clicked"
def __init__(self):
super(self.__class__, self).__init__()
self.setupUi(self)
self.label.mousePressEvent = self.click
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
QLabel inherits QWidget. QWidget` has an event handler mousePressEvent
so as E-ebola virus mentioned
add
self.label.mousePressEvent = self.click
and define
def click(self,event):
#do something
or
subclass QLabel
class customLabel(QLabel):
def __init__(self):
QLabel.__init__(self)
def self.mousePressEvent(self,event):
self.emit(SIGNAL("closeapp"))
emit a SIGNAL when label is pressed, i would implement it as
self.label = customLabel()
self.connect(self.label,SIGNAL('closeapp'),self.close)

How do i connect from main window to next window in pyqt?

im beginner in Pyqt4.So, i need a help. I had do a multiple widget in Pyqt designer, so i want to connect my main window to my widget. In my main menu, there's a enter button and i want when i clicked the enter button, it will go to page2.ui Here is my code
MainWindow.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'MainWindow.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!
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_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(707, 563)
MainWindow.setMinimumSize(QtCore.QSize(707, 563))
MainWindow.setMaximumSize(QtCore.QSize(707, 563))
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap(_fromUtf8("../../Documents/Icon/2.0/TheBat.ico")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
MainWindow.setWindowIcon(icon)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.label_start = QtGui.QLabel(self.centralwidget)
self.label_start.setGeometry(QtCore.QRect(0, 0, 731, 501))
self.label_start.setText(_fromUtf8(""))
self.label_start.setPixmap(QtGui.QPixmap(_fromUtf8("../../Downloads/Workout routine program.jpg")))
self.label_start.setObjectName(_fromUtf8("label_start"))
self.horizontalLayoutWidget = QtGui.QWidget(self.centralwidget)
self.horizontalLayoutWidget.setGeometry(QtCore.QRect(0, 500, 721, 51))
self.horizontalLayoutWidget.setObjectName(_fromUtf8("horizontalLayoutWidget"))
self.horizontalLayout = QtGui.QHBoxLayout(self.horizontalLayoutWidget)
self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
self.enter = QtGui.QPushButton(self.horizontalLayoutWidget)
font = QtGui.QFont()
font.setFamily(_fromUtf8("Corbel"))
font.setPointSize(10)
font.setBold(True)
font.setItalic(False)
font.setUnderline(False)
font.setWeight(75)
font.setStrikeOut(False)
font.setStyleStrategy(QtGui.QFont.PreferAntialias)
self.enter.setFont(font)
self.enter.setAutoFillBackground(False)
icon1 = QtGui.QIcon()
icon1.addPixmap(QtGui.QPixmap(_fromUtf8("../../Documents/Icon/2.0/install.ico")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.enter.setIcon(icon1)
self.enter.setCheckable(False)
self.enter.setAutoDefault(False)
self.enter.setObjectName(_fromUtf8("enter"))
self.horizontalLayout.addWidget(self.enter)
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "Welcome to Workout Routine Program", None))
self.enter.setText(_translate("MainWindow", "Enter", None))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
page2.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'page2_choosegender.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!
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_page2(object):
def setupUi(self, page2):
page2.setObjectName(_fromUtf8("page2"))
page2.setEnabled(True)
page2.resize(627, 459)
self.label_male = QtGui.QLabel(page2)
self.label_male.setGeometry(QtCore.QRect(50, 100, 251, 251))
self.label_male.setText(_fromUtf8(""))
self.label_male.setPixmap(QtGui.QPixmap(_fromUtf8("../../Downloads/1455198182_Tony_Stark.png")))
self.label_male.setObjectName(_fromUtf8("label_male"))
self.label_female = QtGui.QLabel(page2)
self.label_female.setGeometry(QtCore.QRect(330, 100, 251, 251))
self.label_female.setText(_fromUtf8(""))
self.label_female.setPixmap(QtGui.QPixmap(_fromUtf8("../../Downloads/1455198167_Customer_Female_Dark.png")))
self.label_female.setObjectName(_fromUtf8("label_female"))
self.horizontalLayoutWidget = QtGui.QWidget(page2)
self.horizontalLayoutWidget.setGeometry(QtCore.QRect(40, 360, 531, 80))
self.horizontalLayoutWidget.setObjectName(_fromUtf8("horizontalLayoutWidget"))
self.horizontalLayout = QtGui.QHBoxLayout(self.horizontalLayoutWidget)
self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
self.pushButton_male = QtGui.QPushButton(self.horizontalLayoutWidget)
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap(_fromUtf8("../../Documents/Icon/2.0/Male.ico")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.pushButton_male.setIcon(icon)
self.pushButton_male.setObjectName(_fromUtf8("pushButton_male"))
self.horizontalLayout.addWidget(self.pushButton_male)
self.pushButton_female = QtGui.QPushButton(self.horizontalLayoutWidget)
icon1 = QtGui.QIcon()
icon1.addPixmap(QtGui.QPixmap(_fromUtf8("../../Documents/Icon/2.0/Female.ico")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.pushButton_female.setIcon(icon1)
self.pushButton_female.setObjectName(_fromUtf8("pushButton_female"))
self.horizontalLayout.addWidget(self.pushButton_female)
self.textEdit = QtGui.QTextEdit(page2)
self.textEdit.setGeometry(QtCore.QRect(40, 20, 531, 61))
self.textEdit.setReadOnly(True)
self.textEdit.setObjectName(_fromUtf8("textEdit"))
self.retranslateUi(page2)
QtCore.QMetaObject.connectSlotsByName(page2)
def retranslateUi(self, page2):
page2.setWindowTitle(_translate("page2", "Choose your gender", None))
self.pushButton_male.setText(_translate("page2", "Male", None))
self.pushButton_female.setText(_translate("page2", "Female", None))
self.textEdit.setHtml(_translate("page2", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:7.8pt; font-weight:400; font-style:normal;\">\n"
"<p align=\"center\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:20pt; font-weight:600;\">Are you ?</span></p></body></html>", None))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
page2 = QtGui.QWidget()
ui = Ui_page2()
ui.setupUi(page2)
page2.show()
sys.exit(app.exec_())
I hope you guys can help me . Thanks
i wouldn't recommend you to continue the way you're going even the error message at the top warns you that changes in the auto-generate file would be lost if you regenerate it again. you should seperate your Ui file and your main code
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'MainWindow.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
from page2 import Ui_page2
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_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(707, 563)
MainWindow.setMinimumSize(QtCore.QSize(707, 563))
MainWindow.setMaximumSize(QtCore.QSize(707, 563))
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap(_fromUtf8("../../Documents/Icon/2.0/TheBat.ico")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
MainWindow.setWindowIcon(icon)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.label_start = QtGui.QLabel(self.centralwidget)
self.label_start.setGeometry(QtCore.QRect(0, 0, 731, 501))
self.label_start.setText(_fromUtf8(""))
self.label_start.setPixmap(QtGui.QPixmap(_fromUtf8("../../Downloads/Workout routine program.jpg")))
self.label_start.setObjectName(_fromUtf8("label_start"))
self.horizontalLayoutWidget = QtGui.QWidget(self.centralwidget)
self.horizontalLayoutWidget.setGeometry(QtCore.QRect(0, 500, 721, 51))
self.horizontalLayoutWidget.setObjectName(_fromUtf8("horizontalLayoutWidget"))
self.horizontalLayout = QtGui.QHBoxLayout(self.horizontalLayoutWidget)
self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
# initialize the qwidget as an attribute of the class and set a listener on the button
self.page2 = QtGui.QWidget()
ui = Ui_page2()
ui.setupUi(self.page2)
self.enter = QtGui.QPushButton(self.horizontalLayoutWidget)
self.enter.clicked.connect(self.page2.show)
font = QtGui.QFont()
font.setFamily(_fromUtf8("Corbel"))
font.setPointSize(10)
font.setBold(True)
font.setItalic(False)
font.setUnderline(False)
font.setWeight(75)
font.setStrikeOut(False)
font.setStyleStrategy(QtGui.QFont.PreferAntialias)
self.enter.setFont(font)
self.enter.setAutoFillBackground(False)
icon1 = QtGui.QIcon()
icon1.addPixmap(QtGui.QPixmap(_fromUtf8("../../Documents/Icon/2.0/install.ico")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.enter.setIcon(icon1)
self.enter.setCheckable(False)
self.enter.setAutoDefault(False)
self.enter.setObjectName(_fromUtf8("enter"))
self.horizontalLayout.addWidget(self.enter)
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "Welcome to Workout Routine Program", None))
self.enter.setText(_translate("MainWindow", "Enter", None))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
as i said earlier your code is not structured properly, if it was it would be as easy as calling self.hide(), or self.close but your Ui_MainWindow is not a QWidget or QMainWindow so it dosen't have that method the QMainWindow is constructed outside the class so you don't have access to it. with some hacking around it can be fixed but it's not good practice. i'll suggest you look at the git repo https://github.com/shuge/Qt-Python-Binding-Examples and see how to organize your code. ideally the auto-generated ui file should be seperated from your logic...This makes it easier to modify your logic in future without messing up the ui or losing your changes when you generate the ui again
ideally you should inherit from QtGui.QMainWindow or some sort of widget
Your autogenerated ui file
ui.py
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(995, 717)
def retranslateUi(self, MainWindow)
pass
logic.py
from ui import Ui_MainWindow
class MyUi(QtGui.QMainWindow):
def __init__(self, parent=None):
super(criticalPath, self).__init__(parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
main.py
import sys
from logic import MyUi
def main():
app = QtGui.QApplication(sys.argv)
#create a new object
my_interface = MyUi()
my_interface.show()
sys.exit(app.exec_())
if __name__ == '__main__': main()

Categories

Resources