Python Building App - python

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

Related

PyQt5 add eventFilter outside of pyuic generated code

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

PyQt5 - how to avoid crashing gui while updating progresssbar in a multi-threading setting (Qthread)?

I am new in multi threading. The video file I use work fine and the progress-bar at the start shows a percent in progress-bar. When I want to change the progress-bar value continuously for example the current cpu usage value the below code keeps crashing when i run the code. Why is the code not working??
I think the problem is emit and connect. If so what can i do? And how do I correct this? Thanks in advance for the help.
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QObject, pyqtSignal
import sysinfo
from multi import Ui_Form
class main(QtWidgets.QWidget ,Ui_Form):
cpu_value = pyqtSignal()
def __init__(self, parent = None):
super(main,self).__init__(parent)
self.setupUi(self)
self.threadclass = ThreadClass()
self.threadclass.start()
self.cpu_value.connect(self.updateProgressBar)
def updateProgressBar(self):
val = sysinfo.getCPU()
self.progressBar.setValue(val)
class ThreadClass(QtCore.QThread):
def __init__(self, parent = None):
super(ThreadClass,self).__init__(parent)
def run(self):
while 1:
val = sysinfo.getCPU()
self.cpu_value.emit(val)
if __name__ == '__main__':
a = QtWidgets.QApplication(sys.argv)
app = main()
app.show()
a.exec_()
env: python3.5 + pyqt5.9
I use a timer to update the CPU info like that:
#!/usr/bin/python3
# 2017.12.10 13:20:11 CST
# 显示 CPU
import sys
from PyQt5 import QtCore, QtWidgets
import psutil
class CPU(QtWidgets.QProgressBar):
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle("CPU Info")
self.timer = QtCore.QTimer(self)
self.timer.timeout.connect(lambda : self.setValue(psutil.cpu_percent()))
self.timer.start(1000)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
win = CPU()
win.show()
app.exec_()

PyQt5 method not connected to button

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.

Hide the app from screen but not from taskbar

I would like to hide the app from screen but not from taskbar, I tried this:
app = QtWidgets.QApplication([])
w = QtWidgets.QWidget()
w.show()
w.resize(0, 0)
but it doesn't work, any idea?
app = QtWidgets.QApplication([])
w = QtWidgets.QWidget()
w.showMinimized()
I use QMainWindow instead of QWidget, then I override the focusInEvent and focusOutEvent events.
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from PyQt5.QtWidgets import QMainWindow, QApplication
from PyQt5.QtCore import Qt
from sys import argv, exit
class Window(QMainWindow):
def __init__(self):
super(Window, self).__init__()
self.setFocusPolicy(Qt.StrongFocus)
def focusInEvent(self, event):
print('focusInEvent')
self.setWindowTitle('focusInEvent')
self.showMinimized()
def focusOutEvent(self, event):
print('focusOutEvent')
self.setWindowTitle('focusOutEvent')
# self.showMinimized()
if __name__ == '__main__':
app = QApplication([])
w = Window()
w.showMinimized()
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_())

Categories

Resources