Calling pyqt windows - python

I created 2 windows on QT4.
In Idle i managed to show Window 1 and adding a closing action once button is clicked.
I would like to call and display window 2 by clicking this button. My code is:
import os
import shlex
import sys, Tkinter
#import Converted Python UI File
from W0 import Ui_MainWindow1
If i include from W import Ui_MainWindow2 for calling my 2nd windows, it shows directly my window2 and skip window 1!!
class Main(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
# Button close the window1(would like to show window 2 instead now) .
self.ui.pushButton.clicked.connect(self.close)
def main():
app = QtGui.QApplication(sys.argv)
window = Main()
window.show()
sys.exit(app.exec_())
main()

Try this
from W0 import Ui_MainWindow1
from W import Ui_MainWindow2
class Main1(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.ui = Ui_MainWindow1()
self.ui.setupUi(self)
self.ui.pushButton.clicked.connect(self.close)
class Main2(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.ui = Ui_MainWindow2()
self.ui.setupUi(self)
self.ui.pushButton.clicked.connect(self.close)
def main():
app = QtGui.QApplication(sys.argv)
window1 = Main1()
window1.show()
window2 = Main2()
window2.show()
sys.exit(app.exec_())
main()

Related

(PyQt5) How can i show a new window in my main window?

I'm trying to show a new dialog window in my main window when i click on a pushbutton. I'm facing some issues here:
1: when i create a new dialog object inside my mainWindow class, i'll have to use lambda as a slot or else it wont work. the code below works as expected:
class main(QMainWindow):
def __init__(self):
super().__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
new = QDialog()
self.ui.pushButton.clicked.connect(lambda: new.show())
self.show()
app = QApplication(sys.argv)
ex = main()
sys.exit(app.exec_())
while going like self.ui.pushButton.clicked.connect(new.show) does not work.
But if the object is created outside the main class it will run without Lambda:
class main(QMainWindow):
def __init__(self):
super().__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.show()
app = QApplication(sys.argv)
ex = main()
new = QDialog()
ex.ui.pushButton.clicked.connect(new.show)
sys.exit(app.exec_())
I really don't understand what is happening here. AFAIK using a slot without Lambda should work as long as no arguments are passed to it.
2:
If i wanted to connect the pushbutton to a slot (in which i create a dialog object and run the show() method on it), the dialog window would appear and then immediately disappear, in this case i'll need to do dialoginstance.exec_() instead of dialoginstance.show()
class main(QMainWindow):
def __init__(self):
super().__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.pushButton.clicked.connect(lambda: self.showdiag())
self.show()
def showdiag(self):
new = QDialog()
new.exec_() # new.show() wont work!
app = QApplication(sys.argv)
ex = main()
sys.exit(app.exec_())

change QMainWindow PyQt5 after pressed button

I have generated my UI with Qt Designer:
like this
I have used the .ui file with the following python code:
Ui_MainWindow, QtBaseClass = uic.loadUiType("vault.ui")
Ui_Credentials, QtBaseClass = uic.loadUiType("credentials.ui")
class Credentials(QMainWindow):
def __init__(self):
super(Credentials, self).__init__()
self.ui = Ui_Credentials()
self.ui.setupUi(self)
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.load.clicked.connect(self.loadVault)
self.ui.next.clicked.connect(self.next)
self.controller = CLI(....)
self.loadVault()
def loadVault(self):
self.ui.vault.clear()
vaults = self.controller.listVaults()
for vault in vaults:
item = QListWidgetItem(vault)
self.ui.vault.addItem(item)
def next(self):
print(self.ui.vault.currentItem().text())
window = Credentials()
window.show()
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
I have tried to change the window when the button next is pressed by created a new class and using a different ui file.
I found this stackoverflow post where this is a similar problem but the code is this post doesn't use a .ui and I did not manage to have a working code with .ui file. I have succeeded to have a new window when I don't use my ui file.
Someone know how can I deal with this ? Is it not adviced to use .ui file?
The solution that I propose is similar to my previous answer, the objective is to change the graphical part so we will use the function setupUI () that generates that part.
When we press the next button, you must change it back with that function.
Ui_MainWindow, _ = uic.loadUiType("vault.ui")
Ui_Credentials, _ = uic.loadUiType("credentials.ui")
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.startMainWindow()
def startMainWindow(self):
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.next.clicked.connect(self.startCredentials)
def startCredentials(self):
self.ui = Ui_Credentials()
self.ui.setupUi(self)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())

PyQt5 can't use pre defined widgets in separate ui file

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

How to disable a QPushButton

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

PYQT Adding extra menu items to an existing UI before it opens

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

Categories

Resources