When I open the application MyWindow showing and I get some information from the user and clase this window with self.close and open opencv window recording. After a time, record fnishing I try to restart MyWindow (first) process with below codes but it doesnt restart.
When timer end, I am calling below line:
QtGui.qApp.exit(MyWindow.EXIT_CODE_REBOOT)
And this is the main code:
if __name__ == '__main__':
currentExitCode = MyWindow.EXIT_CODE_REBOOT
while currentExitCode == MyWindow.EXIT_CODE_REBOOT:
a = QApplication(sys.argv)
w = MyWindow()
w.show()
currentExitCode = a.exec_()
a = None # delete the QApplication object
and I also have this value:
class MyWindow(QtWidgets.QWidget):
EXIT_CODE_REBOOT = -123
Where is my fault ? How can I restart application ?
Related
I'm working on a basic project with using PyQt5. I made a break method so I can close app with pressing a button. I made a loop in this but when I close the window loop doesn't go back. I want to able to close the window without quitting the program fully.
There is 'main' side:
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
mainWin = TextBar()
mainWin.show()
sys.exit(app.exec_())
And there is button and its command:
pybutton = QPushButton('OK', self)
pybutton.clicked.connect(self.clickMethod)
pybutton.resize(200, 32)
pybutton.move(80, 100)
def clickMethod(self):
print('Name: ' + self.tool.text(), '\nProperties:' + self.prop.text(), '\n')
TextBar.close(self)
I want to close ONLY window when I press OK, NOT full of program.
when the Mainwindow is closed nothing should be running, unless you consider hidding it, like
YourQMainWindow.hide()
self.YourQMainWindow.hide()
2-Methode initialize 2 difference instances, like that once one is closed, another remains or, at about to close, your run a second instance
if __name__ == "__main__":
app = Qtw.QApplication(sys.argv)
main_window = MainWindow() #---->first instance
main_window1 = MainWindow() #---->second instance
sys.exit(app.exec())
if you run then both, they take more ressouces,
so i suggest you create a button to close the first one, that alse start at the same time the second instance of your app,....
Unless you provide more reproductible example (a piece of code), this is how i can only go in advicing you,...(please confirm if it's working for you, me i always use this technic, & it's working),...thank you
I am getting an issue when trying to open a PyQt window.
The code below is an example of my original code. When I imported the module in import Test and ran test.Start(), I got the following error:
QCoreApplication::exec: The event loop is already running
After some research, I found out it was because I had already already made a QApplication.
test.py....
import sys
def Start():
app = QApplication(sys.argv)
m = myWindow()
m.show()
app.exec_()
class myWindow():....
if __name__ == "__main__":
Start()
So then I read that I could rewrite my code like this and it would fix the error:
test.py....
def Start():
m = myWindow()
m.show()
class myWindow():....
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
Start()
app.exec_()
Now I no longer get the QCoreApplication::exec: The event loop is already running error, but my window closes almost immediately after opening.
You need to keep a reference to the opened window, otherwise it goes out of scope and is garbage collected, which will destroy the underlying C++ object also. Try:
def Start():
m = myWindow()
m.show()
return m
class myWindow():....
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
window = Start()
app.exec_()
You can also do:
def Start():
global m
m = myWindow()
m.show()
class myWindow():....
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
window = Start()
app.exec_()
Use the following code. Your problem is in your imports and using "show" as a name for function as far as I assume. You haven't provided what you have written in your class, so it's difficult to guess. But following code works like a charm. ;-)
Best wishes, good luck!
import sys
from PyQt5 import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
app = QApplication(sys.argv)
def Start():
m = myWindow()
m.showWid()
sys.exit(app.exec())
class myWindow:
def __init__(self):
self.window = QWidget()
self.window.setWindowTitle("Program Title")
self.window.setFixedWidth(600)
self.window.setStyleSheet("background: #18BEBE;")
def showWid(self):
self.window.show()
if __name__ == "__main__":
Start()
I'm trying to open a second window in a new process to not freeze the main window with PyQt5. For this reason, I define a new class that inherits from multiprocessing.Process and shows the window. This is the main code:
class GuiMain(QMainWindow):
...
# Main window with several functions. When a button is clicked, executes
# self.button_pressed()
def button_pressed(self):
proc1 = OpenWindowProcess()
proc1.start()
class OpenWindowProcess(mp.Process)
def __init__(self):
mp.Process.__init__(self)
print(self.pid)
def run(self):
print("Opening window...")
window = QtGui.QWindow()
window.show()
time.sleep(10)
if __name__ == '__main__':
app = QApplication(sys.argv)
application = GuiMain()
sys.exit(app.exec_())
The process is created and gets a PID. When run() function is called, the message "Opening window.." is displayed, but nothing else happens. No window, no error... I can't figure out whats happening. Thank you in advance!
I've come to a solution. You have to create a new QtApplication and attach to it a new QMainWindow instance. This code works fine:
class GuiMain(QMainWindow):
...
# Main window with several functions. When a button is clicked, executes
# self.button_pressed()
def button_pressed(self):
proc1 = OpenWindowProcess()
proc1.start()
class OpenWindowProcess(mp.Process)
def __init__(self):
mp.Process.__init__(self)
print("Process PID: " + self.pid)
def run(self):
print("Opening window...")
app = QApplication(sys.argv)
window = QMainWindow()
window.show()
sys.exit(app.exec_())
if __name__ == '__main__':
app = QApplication(sys.argv)
application = GuiMain()
sys.exit(app.exec_())
I would like to execute "onFocus" function when the app is focused, due to when I navigate from apps with ALT+TAB it shows the window, the code:
def onFocus(w):
w.showMinimized()
# another stuff
if __name__ == '__main__':
app = QtWidgets.QApplication([])
w = QtWidgets.QWidget()
onFocus(w)
exit(app.exec_())
What is the event for do this? Thank you very much
I can't input anything from the user in the terminal while running a PyQt Application.
Actually i am creating something but i can't show the whole code here so there is the main of the code, and believe that i need the fix of it only:
from PyQt4.QtGui import *
import sys
def window():
app = QApplication(sys.argv)
window = QWidget()
btn = QPushButton()
btn.setText("Input In Console")
box = QFormLayout()
box.addRow(btn)
btn.clicked.connect(input_txt)
window.setLayout(box)
window.show()
sys.exit(app.exec_())
def input_txt():
input("Enter you Name ")
if __name__ == "__main__":
window()
And while running as i press the button a disaster of loop starts:
I really tried a lot configuring the solution of this problem but all failed. Hope these information helped, if any question regarding the post then please say in comment.
I don't need this anymore but still i am posting answer for if someone else having problem with the same.
Solution : Use Threads
from PyQt4.QtGui import *
import sys,threading
def window():
app = QApplication(sys.argv)
window = QWidget()
btn = QPushButton()
btn.setText("Input In Console")
box = QFormLayout()
box.addRow(btn)
btn.clicked.connect(input_txt)
window.setLayout(box)
window.show()
sys.exit(app.exec_())
def input_txt():
thread = threading.Thread(target=input)
thread.start()
if __name__ == "__main__":
window()