This is my code for running PyQt, however the selectFile method is not called by the button. The UI code is converted from QtCreator. I've checked my objectName for the button is browseCSV
import sys
from readCSV import *
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QFileDialog
import form
from function2 import *
from function4 import *
from Function6 import *
class App(QtWidgets.QMainWindow, form.Ui_MainWindow):
def __init__(self):
super(self.__class__, self).__init__()
self.setupUi(self) # This is defined in design.py file automatically
self.browseCSV.clicked.connect(self.selectFile)
def selectFile(self):
print ("Hello")
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = form.Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
You're not actually using your App class. So you need to do this:
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = App()
window.show()
sys.exit(app.exec_()
PS: don't ever use self.__class__ in a super call. In some scenarios, it can cause an infinite regress. If you're using Python 3, you can just use super().__init__() to avoid repeating the class name.
Related
Main interacts with a GUI defined by a separate module. Two alternative approaches are used in Main to define the slot for a QShortcut. The lambda method works (but seems cumbersome). The direct method works, only if the lambda method is executed first. If the line with the lambda method is commented out, then the direct method fails (with no error message). Why does the direct method alone fail?
'''
# p.py
import sys
from PyQt5 import QtWidgets as qtw
from Q import MainWindow
class MyActions():
def __init__(self, my_window):
self.my_window = my_window
self.my_window.run.activated.connect(lambda: self.rmssg1()) # had to use this as a work-around
self.my_window.run.activated.connect(self.rmssg2) # expected this to work
def rmssg1(self):
self.my_window.my_label1.setText('Ctrl+R pressed -> mssg 1')
def rmssg2(self):
self.my_window.my_label2.setText('Ctrl+R pressed -> mssg 2')
if __name__ == '__main__':
app = qtw.QApplication(sys.argv)
mw = MainWindow()
MyActions(mw)
mw.show()
sys.exit(app.exec())
'''
Here's the GUI in a separate module
'''
#q.py
import sys
from PyQt5 import QtWidgets as qtw
from PyQt5.QtGui import QKeySequence
class MainWindow(qtw.QMainWindow):
def __init__(self, parent=None):
super().__init__()
self.my_label1 = qtw.QLabel("Enter Ctrl+R", self)
self.my_label1.setGeometry(20,20,200,30)
self.my_label2 = qtw.QLabel("Enter Ctrl+R", self)
self.my_label2.setGeometry(20, 50, 200, 30)
#define shortcut
self.run = qtw.QShortcut(QKeySequence('Ctrl+R'), self)
'''
The problem is simple: When you do MyActions(mw) not assign a variable to the object so memory can be lost, the solution is:
if __name__ == '__main__':
app = qtw.QApplication(sys.argv)
mw = MainWindow()
foo = MyActions(mw)
mw.show()
sys.exit(app.exec())
I have multiple windows in a Python GUI application using PyQt5.
I need to hide current window when a button is clicked and show the next window.
This works fine from WindowA to WindowB but I get an error while going from WindowB to WindowC.
I know there is some problem in initialization as the initialization code in WindowB is unreachable, but being a beginner with PyQt, i can't figure out the solution.
WindowA code:
from PyQt5 import QtCore, QtGui, QtWidgets
from WindowB import Ui_forWindowB
class Ui_forWindowA(object):
def setupUi(self, WindowA):
# GUI specifications statements here
self.someButton = QtWidgets.QPushButton(self.centralwidget)
self.someButton.clicked.connect(self.OpenWindowB)
# More GUI specifications statements here
def retranslateUi(self, WindowA):
# More statements here
def OpenWindowB(self):
self.window = QtWidgets.QMainWindow()
self.ui = Ui_forWindowB()
self.ui.setupUi(self.window)
WindowA.hide()
self.window.show()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
WindowA = QtWidgets.QMainWindow()
ui = Ui_forWindowA()
ui.setupUi(WindowA)
MainWindow.show()
sys.exit(app.exec_())
WindowB code:
from PyQt5 import QtCore, QtGui, QtWidgets
from WindowB import Ui_forWindowB
class Ui_forWindowB(object):
def setupUi(self, WindowB):
# GUI specifications statements here
self.someButton = QtWidgets.QPushButton(self.centralwidget)
self.someButton.clicked.connect(self.OpenWindowC)
# More GUI specifications statements here
def retranslateUi(self, WindowB):
# More statements here
def OpenWindowB(self):
self.window = QtWidgets.QMainWindow()
self.ui = Ui_forWindowC()
self.ui.setupUi(self.window)
WindowB.hide() # Error here
self.window.show()
# The below code doesn't get executed when Ui_forWindowB is called from A
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
WindowB = QtWidgets.QMainWindow()
ui = Ui_forWindowB()
ui.setupUi(WindowB)
MainWindow.show()
sys.exit(app.exec_())
It works fine from A to B where
WindowA.hide() # Works Properly
While calling WindowC from WindowB
WindowB.hide() # Shows error: name 'WindowB' is not defined
I understand that the initialization isn't done as the "if" statement doesn't get executed.
How to get this working?
I have many more windows to connect in this flow
When you run a Python script, the first file executed will be assigned the name __main__, therefore, if you first execute WindowA the code inside the block if __name__ == "__main__" gets executed and the application is started using WindowA as the main window, similarly if you execute your WindowB script first, the application will be started usingWindowB as the main window.
You cannot start two applications within the same process so you have to choose which one you want to be the main window, all the others will be secondary windows (even if they inherit from QMainWindow).
Nevertheless, you should be able to instantiate new windows from a method in your main window.
As a good practice, you could create a main script to handle the initialization of your application and start an empty main window that will then handle your workflow, also, you may want to wrap your UI classes, specially if they are generated using Qt creator, here is an example:
main.py
import PyQt5
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QApplication
from views.main_window import MainWindow
class App(QApplication):
"""Main application wrapper, loads and shows the main window"""
def __init__(self, sys_argv):
super().__init__(sys_argv)
# Show main window
self.main_window = MainWindow()
self.main_window.show()
if __name__ == '__main__':
app = App(sys.argv)
sys.exit(app.exec_())
main_window.py
This is the main window, it doesn't do anything, just control the workflow of the application, i.e. load WindowA, then WindowB etc., notice that I inherit from Ui_MainWindow, by doing so, you can separate the look and feel from the logic and use Qt Creator to generate your UI:
from PyQt5.QtWidgets import QWidget, QMainWindow
from views.window_a import WindowA
from views.window_b import WindowB
from widgets.main_window import Ui_MainWindow
class MainWindow(Ui_MainWindow, QMainWindow):
"""Main application window, handles the workflow of secondary windows"""
def __init__(self):
Ui_MainWindow.__init__(self)
QMainWindow.__init__(self)
self.setupUi(self)
# start hidden
self.hide()
# show window A
self.window_a = WindowA()
self.window_a.actionExit.triggered.connect(self.window_a_closed)
self.window_a.show()
def window_a_closed(self):
# Show window B
self.window_b = WindowB()
self.window_b.actionExit.triggered.connect(self.window_b_closed)
self.window_b.show()
def window_b_closed(self):
#Close the application if window B is closed
self.close()
window_a.py
from PyQt5.QtWidgets import QWidget, QMainWindow
from widgets.main_window import Ui_forWindowA
class WindowA(Ui_forWindowA, QMainWindow):
"""Window A"""
def __init__(self):
Ui_forWindowA.__init__(self)
QMainWindow.__init__(self)
self.setupUi(self)
# Do some stuff
window_b.py
from PyQt5.QtWidgets import QWidget, QMainWindow
from widgets.main_window import Ui_forWindowB
class WindowA(Ui_forWindowB, QMainWindow):
"""Window B"""
def __init__(self):
Ui_forWindowB.__init__(self)
QMainWindow.__init__(self)
self.setupUi(self)
# Do some stuff
Hopefully should give you an idea to get you going.
So here is the problem:
I am building an app in pyqt:
frame1.py
from PyQt5 import QtWidgets, QtCore, QtGui
import runpy, sys
class LoginFrame(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.logFrameSetup(self)
def logFrameSetup(self, GuiWin):
GuiWin.setWindowTitle('GuiWin')
GuiWin.resize(450, 215)
self.pushbutton = QtWidgets.QPushButton('Go To', GuiWin)
self.pushbutton.clicked.connect(self.change)
def change(self):
try:
runpy.run_path('frame2.py', run_name="__main__")
except:
pass
finally:
self.close()
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
ex = LoginFrame()
ex.show()
x = app.exec_()
sys.exit(x)
frame2.py
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
class MainTradingPlatform(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.setupFrame(self)
def setupFrame(self, frame2):
frame2.setWindowTitle('2frame')
frame2.resize(1200, 1000)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
ex = MainTradingPlatform()
ex.show()
x = app.exec_()
sys.exit(x)
Now this works almost perfect, but when i close frame2.py, the process in background is still in progress/running.
What I want to achieve is that by closing frame2.py, process is finished with exit code. (terminated)
ps: after calling frame2.py, i would also like to terminate frame1.
Thank you for your help and sorry for my response.
When you execute the code with runpy, the process is separated from the initial application, an option to close the second application is to overwrite the closeEvent method.
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
class MainTradingPlatform(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.setupFrame(self)
def setupFrame(self, frame2):
frame2.setWindowTitle('2frame')
frame2.resize(1200, 1000)
def closeEvent(self, e):
sys.exit(0)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
ex = MainTradingPlatform()
ex.show()
sys.exit(app.exec_())
I am trying to separate UI components from functional code as much as I can, so my PySide is application structured like this
main.py
package\
app.py
__init__.py
ui\
mainwindow.py
__init__.py
Main is just a simple starter
if __name__ == '__main__':
import sys
from package import app
sys.exit(app.run())
and app is where all the functionality should reside. I start the UI from app.py
from PySide import QtCore, QtGui
#QtCore.Slot()
def start_button_clicked():
print "started"
def run():
import sys
from ui.mainwindow import Ui_MainWindow
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Now, from the user interface I want to connect the emitted signals up to the app.py to avoid having a lot of functionality cluttering the mainwindow file, but the UI file is not aware of app.py. How should I go forth to do this and avoid all slots being in mainwindow.py? app.py should easily be able to do stuff on the UI since it has a object reference to it, but I have no clue on the other way around.
Create a subclass for the top-level widget from Qt Designer. Using this approach, all of child widgets from Qt Designer will become attributes of the subclass:
import sys
from PySide import QtCore, QtGui
from ui.mainwindow import Ui_MainWindow
class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.setupUi(self)
self.start_button.clicked.connect(self.start_button_clicked)
def start_button_clicked(self):
print "started"
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
window = MainWindow()
window.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()