I tried calling UI from another file, but couldn't disable a button. I don't know where to place .setEnabled(False). I placed it everywhere except main, but the button is still enabled.
import sys
from PyQt4 import QtCore, QtGui
from a import Ui_MainWindow
class machine(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self):
super(machine, self).__init__()
self.setupUi(self)
self.initUI()
self.disablebtn()
self.btn_Save.setEnabled(False);
self.btn_Close.setEnabled(False);
self.show()
def initUI(self):
self.desktopSize()
self.statusbar().showMessage("super dooper")
self.btn_Save.setEnabled(False);
self.btn_Close.setEnabled(False);
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_())
Your machine class does disable the buttons correctly, but you never create an instance of it, and so it never gets a chance to work properly.
The code should probably look more like this:
import sys
from PyQt4 import QtCore, QtGui
from a import Ui_MainWindow
class machine(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self):
super(machine, self).__init__()
self.setupUi(self)
self.initUI()
def initUI(self):
self.statusBar().showMessage("super dooper")
self.btn_Save.setEnabled(False)
self.btn_Close.setEnabled(False)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
MainWindow = machine()
MainWindow.show()
sys.exit(app.exec_())
Related
I'm a new Qt user here.
I've got a project where I'm to use a pyuic generated .py file but I don't have access to it.
I'm also supposed to be installing event filters on some of the objects. Is it possible to use object.installEventFilter() outside the generated .py file?
main_window.py
class Ui_MainWindow(QtWidgets.QMainWindow):
self.titleLabel = QtWidgets.QLabel(MainWindow)
Frontend.py
from PyQt5 import QtCore, QtGui, QtWidgets
from main_window import Ui_MainWindow
class Session (object):
def __init__(self):
self.mainUI = None
def eventFilter(self, source, event):
eventReturn = False
if(event.type() == QtCore.QEvent.MouseButtonDblClick and
source is self.lblTitle):
eventReturn = self._labelTitle(source, event)
return eventReturn
def _labelTitle(self, widget, event):
retVal = True
print("works, Title")
def GUIcontroller():
import sys
app = QtWidgets.QApplication(sys.argv)
thisSession = Session()
MainWindow = QtWidgets.QMainWindow()
thisSession.mainUI = Ui_MainWindow()
thisSession.mainUI.setupUi(MainWindow)
thisSession.mainUI.titleLabel.installEventFilter(???)
MainWindow.show()
sys.exit(app.exec_())
if __name__ == "__main__":
GUIcontroller()
The event filters only work in the QObjects, and in your code you use object that will not work, considering the above a possible solution is:
from PyQt5 import QtCore, QtGui, QtWidgets
from main_window import Ui_MainWindow
class Session(QtCore.QObject):
def __init__(self, ui):
super().__init__(ui)
self._ui = ui
self.ui.installEventFilter(self)
#property
def ui(self):
return self._ui
def eventFilter(self, source, event):
if event.type() == QtCore.QEvent.MouseButtonDblClick and source is self.ui:
print("double clicked")
return super().eventFilter(source, event)
def GUIcontroller():
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
mainUI = Ui_MainWindow()
mainUI.setupUi(MainWindow)
thisSession = Session(mainUI.titleLabel)
MainWindow.show()
sys.exit(app.exec_())
if __name__ == "__main__":
GUIcontroller()
I'm just getting started with PyQt5. I have been trying to accomplish a seemingly very simple task but haven't been able to get enough info about it. After a fair bit of googling I have been able to get one window to close and another to launch with the other UI loaded but that's not what I want to do here.
I want to switch the UI in the same window. I am loading the UI files as global variables in my python file where I have 2 classes for each UI. When I click a particular button in one UI, I want to switch to the other UI in the same window. Below is a sample of the code:
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys
from PyQt5.uic import loadUiType
import os
about_company_ui, _ = loadUiType(os.path.join('frontend', 'ui', 'about_company.ui'))
intern_placement_ui, _ = loadUiType(os.path.join('frontend', 'ui', 'intern_placement.ui'))
class InternPlacement(QMainWindow, intern_placement_ui):
def __init__(self):
QMainWindow.__init__(self)
self.setupUi(self)
self.intern_pushButton.clicked.connect(self.change)
def change(self):
self.about_company = AboutCompany()
self.about_company.show()
self.close()
class AboutCompany(QMainWindow, about_company_ui):
def __init__(self):
QMainWindow.__init__(self)
self.setupUi(self)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = InternPlacement()
window.show()
app.exec_()
You have to use a QStackedWidget
import os
import sys
from PyQt5 import QtCore, QtGui, QtWidgets, uic
ui_folder = os.path.join("frontend", "ui")
about_company_ui, _ = uic.loadUiType(os.path.join(ui_folder, "about_company.ui"))
intern_placement_ui, _ = uic.loadUiType(os.path.join(ui_folder, "intern_placement.ui"))
class InternPlacement(QtWidgets.QMainWindow, intern_placement_ui):
def __init__(self, parent=None):
super(InternPlacement, self).__init__(parent)
self.setupUi(self)
class AboutCompany(QtWidgets.QMainWindow, about_company_ui):
def __init__(self, parent=None):
super(AboutCompany, self).__init__(parent)
self.setupUi(self)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
intern_window = InternPlacement()
about_window = AboutCompany()
w = QtWidgets.QStackedWidget()
w.addWidget(intern_window)
w.addWidget(about_window)
intern_window.intern_pushButton.clicked.connect(lambda: w.setCurrentIndex(1))
w.resize(640, 480)
w.show()
sys.exit(app.exec_())
I am quite new to python. For my problem I cannot seem to find an answer. And sadly I didn't find anything that could held me. I try to call a function from another file in the same folder but it won't work. If I call the same function from the same file, it works just fine. The same goes for external classes.
This code works:
import sys
from PyQt5 import QtWidgets
from ui.mainwindow import Ui_MainWindow
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
QtWidgets.QMainWindow.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.show()
self.ui.pushButton.clicked.connect(test_function)
def test_function():
window.ui.stackedWidget.setCurrentIndex(1)
if __name__=='__main__':
app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
sys.exit(app.exec_())
The following code won't work:
import sys
from PyQt5 import QtWidgets
from ui.mainwindow import Ui_MainWindow
from other_file import test_function
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
QtWidgets.QMainWindow.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.show()
self.ui.pushButton.clicked.connect(test_function)
if __name__=='__main__':
app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
sys.exit(app.exec_())
other_file.py
def test_function():
window.ui.stackedWidget.setCurrentIndex(1)
Error: cannot import name 'test_function'
I'm having a problem with PyQt5 where i have a separate ui file(still a python file not .ui) I'm trying to connect a button which would be located in that file however this doesn't work for me for some reason.
Here's my code.
from PyQt5 import QtCore, QtGui, QtWidgets
from gui import Ui_Form
class Main(QtWidgets.QMainWindow):
def __init__(self):
super(Main, self).__init__()
self.ui = Ui_Form()
self.ui.setupUi(self)
self.show()
self.Ui_Form.exit.clicked.connect(self.handle)
def handle(self):
self.print("hello")
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
and here's some code from my auto generated gui file using pyuic:
self.exit = QtWidgets.QPushButton(Form)
self.exit.setGeometry(QtCore.QRect(375, 270, 115, 27))
self.exit.setObjectName("exit")
this same exact procedure has worked for me before in Qt4 so i don't see why it wouldn't work here?
You must use the ui attribute to access the button. You must change:
self.Ui_Form.exit.clicked.connect(self.handle)
to:
self.ui.exit.clicked.connect(self.handle)
Note: Typically when using a Widget template, it names that element as a form and the design class as Ui_Form, so you should use QWidget as a class base.
Complete code:
class Main(QtWidgets.QWidget):
def __init__(self):
super(Main, self).__init__()
self.ui = Ui_Form()
self.ui.setupUi(self)
self.show()
self.ui.exit.clicked.connect(self.handle)
def handle(self):
self.print("hello")
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = Main()
w.show()
sys.exit(app.exec_())
Hi I have designed a basic GUI in QT and created a .py file from it.
When the window starts up I want to add another menu item. I have tried a few pieces of code I found on google but nothing seems to work. The code will need to go in the method addAdminMenu()
from PyQt4 import QtGui
import sys
from supplypy.core.windows.main_window import Ui_MainWindow
class SRM(QtGui.QWidget):
def __init__(self):
self.app = QtGui.QApplication(sys.argv)
self.MainWindow = QtGui.QMainWindow()
self.ui = Ui_MainWindow()
self.ui.setupUi(self.MainWindow)
self.MainWindow.show()
sys.exit(self.app.exec_())
def addAdminMenu(self):
pass
#####Add code here to create a Admin menu####
if __name__ == '__main__':
srm = SRM()
It should be as simple as accessing the menuBar() of the QMainWindow and adding an item, for example: (I removed the Ui_MainWindow lines just because I don't know what it's for -- a Windows requirement?)
from PyQt4 import QtGui
import sys
class SRM(QtGui.QWidget):
def __init__(self):
self.app = QtGui.QApplication(sys.argv)
self.MainWindow = QtGui.QMainWindow()
self.menubar = self.MainWindow.menuBar()
self.MainWindow.show()
self.addAdminMenu()
sys.exit(self.app.exec_())
def addAdminMenu(self):
self.menubar.addMenu('&Admin');
if __name__ == '__main__':
srm = SRM()