PyQt5: Child window is not displayed until while loop is executed
I have a main class that displays a window with a push button. On clicking the push button calls a child class (that contains while loop) that displays "Yay" in a separate window. I am not sure why the child window is displayed after executing the while loop. Attached is the code for your reference. Any help is much appreciated.
class test1(QtWidgets.QMainWindow):
def __init__(self):
super (test1, self).__init__()
test1.setGeometry(self, 20, 20, 400, 250)
btn = QtWidgets.QPushButton("Add", self)
btn.clicked.connect(self.Check)
btn.resize(btn.sizeHint())
btn.move(10, 170)
self.show()
def Check(self):
self.test2=test2()
class test2(QtWidgets.QMainWindow):
def __init__(self):
super (test2, self).__init__()
test2.setGeometry(self,20, 20, 450, 100)
self.label = QtWidgets.QLabel("Yay",self)
self.show()
a=50
b=60
x=True
time.sleep(5)
while (x):
if a!=b:
a+=1
else:
x=False
def run():
app = QtWidgets.QApplication(sys.argv)
GUI = test1()
GUI.show()
sys.exit(app.exec_())
run()
python pyqt5
add a comment |
I have a main class that displays a window with a push button. On clicking the push button calls a child class (that contains while loop) that displays "Yay" in a separate window. I am not sure why the child window is displayed after executing the while loop. Attached is the code for your reference. Any help is much appreciated.
class test1(QtWidgets.QMainWindow):
def __init__(self):
super (test1, self).__init__()
test1.setGeometry(self, 20, 20, 400, 250)
btn = QtWidgets.QPushButton("Add", self)
btn.clicked.connect(self.Check)
btn.resize(btn.sizeHint())
btn.move(10, 170)
self.show()
def Check(self):
self.test2=test2()
class test2(QtWidgets.QMainWindow):
def __init__(self):
super (test2, self).__init__()
test2.setGeometry(self,20, 20, 450, 100)
self.label = QtWidgets.QLabel("Yay",self)
self.show()
a=50
b=60
x=True
time.sleep(5)
while (x):
if a!=b:
a+=1
else:
x=False
def run():
app = QtWidgets.QApplication(sys.argv)
GUI = test1()
GUI.show()
sys.exit(app.exec_())
run()
python pyqt5
add a comment |
I have a main class that displays a window with a push button. On clicking the push button calls a child class (that contains while loop) that displays "Yay" in a separate window. I am not sure why the child window is displayed after executing the while loop. Attached is the code for your reference. Any help is much appreciated.
class test1(QtWidgets.QMainWindow):
def __init__(self):
super (test1, self).__init__()
test1.setGeometry(self, 20, 20, 400, 250)
btn = QtWidgets.QPushButton("Add", self)
btn.clicked.connect(self.Check)
btn.resize(btn.sizeHint())
btn.move(10, 170)
self.show()
def Check(self):
self.test2=test2()
class test2(QtWidgets.QMainWindow):
def __init__(self):
super (test2, self).__init__()
test2.setGeometry(self,20, 20, 450, 100)
self.label = QtWidgets.QLabel("Yay",self)
self.show()
a=50
b=60
x=True
time.sleep(5)
while (x):
if a!=b:
a+=1
else:
x=False
def run():
app = QtWidgets.QApplication(sys.argv)
GUI = test1()
GUI.show()
sys.exit(app.exec_())
run()
python pyqt5
I have a main class that displays a window with a push button. On clicking the push button calls a child class (that contains while loop) that displays "Yay" in a separate window. I am not sure why the child window is displayed after executing the while loop. Attached is the code for your reference. Any help is much appreciated.
class test1(QtWidgets.QMainWindow):
def __init__(self):
super (test1, self).__init__()
test1.setGeometry(self, 20, 20, 400, 250)
btn = QtWidgets.QPushButton("Add", self)
btn.clicked.connect(self.Check)
btn.resize(btn.sizeHint())
btn.move(10, 170)
self.show()
def Check(self):
self.test2=test2()
class test2(QtWidgets.QMainWindow):
def __init__(self):
super (test2, self).__init__()
test2.setGeometry(self,20, 20, 450, 100)
self.label = QtWidgets.QLabel("Yay",self)
self.show()
a=50
b=60
x=True
time.sleep(5)
while (x):
if a!=b:
a+=1
else:
x=False
def run():
app = QtWidgets.QApplication(sys.argv)
GUI = test1()
GUI.show()
sys.exit(app.exec_())
run()
python pyqt5
python pyqt5
asked Nov 21 '18 at 4:25
KrishKrish
82
82
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
The GUI lives in an event-loop, this a loop that allows to obtain data of diverse elements like the keyboard, the mouse, the logic, etc and to update the status of the GUI, so if the GUI is frozen it will not allow to do its I work correctly. And in this case the one that blocks the event-loop is the sleep, the while does not intervene since it is executed in less than 1 ms.
So the tasks that take up a lot of time must be executed in a different thread of the GUI avoiding that it is blocked, but in your case the sleep can be replaced by a QTimer.
In conclusion, the sleep is freezing the GUI. I see that the code has an educational purpose so I will show you several options:
threading.Thread():
import threading
# ...
class test2(QtWidgets.QMainWindow):
def __init__(self):
super (test2, self).__init__()
test2.setGeometry(self,20, 20, 450, 100)
self.label = QtWidgets.QLabel("Yay",self)
self.show()
threading.Thread(target=self.foo, daemon=True).start()
def foo(self):
time.sleep(5)
a=50
b=60
x=True
while x:
if a != b:
a += 1
print(a)
else:
x = False
QTimer + QEventLoop:
class test2(QtWidgets.QMainWindow):
def __init__(self):
super (test2, self).__init__()
test2.setGeometry(self,20, 20, 450, 100)
self.label = QtWidgets.QLabel("Yay",self)
self.show()
loop = QtCore.QEventLoop()
QtCore.QTimer.singleShot(5*1000, loop.quit)
loop.exec_()
a=50
b=60
x=True
while x:
if a != b:
a += 1
print(a)
else:
x = False
QTimer:
class test2(QtWidgets.QMainWindow):
def __init__(self):
super (test2, self).__init__()
test2.setGeometry(self,20, 20, 450, 100)
self.label = QtWidgets.QLabel("Yay",self)
self.show()
QtCore.QTimer.singleShot(5*1000, self.foo)
def foo(self):
a=50
b=60
x=True
while x:
if a != b:
a += 1
print(a)
else:
x = False
As a general conclusion, the tasks executed in the thread of the GUI should consume a time less than 30ms, if it is greater than that it is recommended to execute it in another thread. In the case that each task within the loop fulfills the above but there are many iterations, a QTimer can be used to perform the tasks of the loop periodically.
it worked. Thank you for the detailed explanation.
– Krish
Nov 21 '18 at 5:18
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53405246%2fpyqt5-child-window-is-not-displayed-until-while-loop-is-executed%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The GUI lives in an event-loop, this a loop that allows to obtain data of diverse elements like the keyboard, the mouse, the logic, etc and to update the status of the GUI, so if the GUI is frozen it will not allow to do its I work correctly. And in this case the one that blocks the event-loop is the sleep, the while does not intervene since it is executed in less than 1 ms.
So the tasks that take up a lot of time must be executed in a different thread of the GUI avoiding that it is blocked, but in your case the sleep can be replaced by a QTimer.
In conclusion, the sleep is freezing the GUI. I see that the code has an educational purpose so I will show you several options:
threading.Thread():
import threading
# ...
class test2(QtWidgets.QMainWindow):
def __init__(self):
super (test2, self).__init__()
test2.setGeometry(self,20, 20, 450, 100)
self.label = QtWidgets.QLabel("Yay",self)
self.show()
threading.Thread(target=self.foo, daemon=True).start()
def foo(self):
time.sleep(5)
a=50
b=60
x=True
while x:
if a != b:
a += 1
print(a)
else:
x = False
QTimer + QEventLoop:
class test2(QtWidgets.QMainWindow):
def __init__(self):
super (test2, self).__init__()
test2.setGeometry(self,20, 20, 450, 100)
self.label = QtWidgets.QLabel("Yay",self)
self.show()
loop = QtCore.QEventLoop()
QtCore.QTimer.singleShot(5*1000, loop.quit)
loop.exec_()
a=50
b=60
x=True
while x:
if a != b:
a += 1
print(a)
else:
x = False
QTimer:
class test2(QtWidgets.QMainWindow):
def __init__(self):
super (test2, self).__init__()
test2.setGeometry(self,20, 20, 450, 100)
self.label = QtWidgets.QLabel("Yay",self)
self.show()
QtCore.QTimer.singleShot(5*1000, self.foo)
def foo(self):
a=50
b=60
x=True
while x:
if a != b:
a += 1
print(a)
else:
x = False
As a general conclusion, the tasks executed in the thread of the GUI should consume a time less than 30ms, if it is greater than that it is recommended to execute it in another thread. In the case that each task within the loop fulfills the above but there are many iterations, a QTimer can be used to perform the tasks of the loop periodically.
it worked. Thank you for the detailed explanation.
– Krish
Nov 21 '18 at 5:18
add a comment |
The GUI lives in an event-loop, this a loop that allows to obtain data of diverse elements like the keyboard, the mouse, the logic, etc and to update the status of the GUI, so if the GUI is frozen it will not allow to do its I work correctly. And in this case the one that blocks the event-loop is the sleep, the while does not intervene since it is executed in less than 1 ms.
So the tasks that take up a lot of time must be executed in a different thread of the GUI avoiding that it is blocked, but in your case the sleep can be replaced by a QTimer.
In conclusion, the sleep is freezing the GUI. I see that the code has an educational purpose so I will show you several options:
threading.Thread():
import threading
# ...
class test2(QtWidgets.QMainWindow):
def __init__(self):
super (test2, self).__init__()
test2.setGeometry(self,20, 20, 450, 100)
self.label = QtWidgets.QLabel("Yay",self)
self.show()
threading.Thread(target=self.foo, daemon=True).start()
def foo(self):
time.sleep(5)
a=50
b=60
x=True
while x:
if a != b:
a += 1
print(a)
else:
x = False
QTimer + QEventLoop:
class test2(QtWidgets.QMainWindow):
def __init__(self):
super (test2, self).__init__()
test2.setGeometry(self,20, 20, 450, 100)
self.label = QtWidgets.QLabel("Yay",self)
self.show()
loop = QtCore.QEventLoop()
QtCore.QTimer.singleShot(5*1000, loop.quit)
loop.exec_()
a=50
b=60
x=True
while x:
if a != b:
a += 1
print(a)
else:
x = False
QTimer:
class test2(QtWidgets.QMainWindow):
def __init__(self):
super (test2, self).__init__()
test2.setGeometry(self,20, 20, 450, 100)
self.label = QtWidgets.QLabel("Yay",self)
self.show()
QtCore.QTimer.singleShot(5*1000, self.foo)
def foo(self):
a=50
b=60
x=True
while x:
if a != b:
a += 1
print(a)
else:
x = False
As a general conclusion, the tasks executed in the thread of the GUI should consume a time less than 30ms, if it is greater than that it is recommended to execute it in another thread. In the case that each task within the loop fulfills the above but there are many iterations, a QTimer can be used to perform the tasks of the loop periodically.
it worked. Thank you for the detailed explanation.
– Krish
Nov 21 '18 at 5:18
add a comment |
The GUI lives in an event-loop, this a loop that allows to obtain data of diverse elements like the keyboard, the mouse, the logic, etc and to update the status of the GUI, so if the GUI is frozen it will not allow to do its I work correctly. And in this case the one that blocks the event-loop is the sleep, the while does not intervene since it is executed in less than 1 ms.
So the tasks that take up a lot of time must be executed in a different thread of the GUI avoiding that it is blocked, but in your case the sleep can be replaced by a QTimer.
In conclusion, the sleep is freezing the GUI. I see that the code has an educational purpose so I will show you several options:
threading.Thread():
import threading
# ...
class test2(QtWidgets.QMainWindow):
def __init__(self):
super (test2, self).__init__()
test2.setGeometry(self,20, 20, 450, 100)
self.label = QtWidgets.QLabel("Yay",self)
self.show()
threading.Thread(target=self.foo, daemon=True).start()
def foo(self):
time.sleep(5)
a=50
b=60
x=True
while x:
if a != b:
a += 1
print(a)
else:
x = False
QTimer + QEventLoop:
class test2(QtWidgets.QMainWindow):
def __init__(self):
super (test2, self).__init__()
test2.setGeometry(self,20, 20, 450, 100)
self.label = QtWidgets.QLabel("Yay",self)
self.show()
loop = QtCore.QEventLoop()
QtCore.QTimer.singleShot(5*1000, loop.quit)
loop.exec_()
a=50
b=60
x=True
while x:
if a != b:
a += 1
print(a)
else:
x = False
QTimer:
class test2(QtWidgets.QMainWindow):
def __init__(self):
super (test2, self).__init__()
test2.setGeometry(self,20, 20, 450, 100)
self.label = QtWidgets.QLabel("Yay",self)
self.show()
QtCore.QTimer.singleShot(5*1000, self.foo)
def foo(self):
a=50
b=60
x=True
while x:
if a != b:
a += 1
print(a)
else:
x = False
As a general conclusion, the tasks executed in the thread of the GUI should consume a time less than 30ms, if it is greater than that it is recommended to execute it in another thread. In the case that each task within the loop fulfills the above but there are many iterations, a QTimer can be used to perform the tasks of the loop periodically.
The GUI lives in an event-loop, this a loop that allows to obtain data of diverse elements like the keyboard, the mouse, the logic, etc and to update the status of the GUI, so if the GUI is frozen it will not allow to do its I work correctly. And in this case the one that blocks the event-loop is the sleep, the while does not intervene since it is executed in less than 1 ms.
So the tasks that take up a lot of time must be executed in a different thread of the GUI avoiding that it is blocked, but in your case the sleep can be replaced by a QTimer.
In conclusion, the sleep is freezing the GUI. I see that the code has an educational purpose so I will show you several options:
threading.Thread():
import threading
# ...
class test2(QtWidgets.QMainWindow):
def __init__(self):
super (test2, self).__init__()
test2.setGeometry(self,20, 20, 450, 100)
self.label = QtWidgets.QLabel("Yay",self)
self.show()
threading.Thread(target=self.foo, daemon=True).start()
def foo(self):
time.sleep(5)
a=50
b=60
x=True
while x:
if a != b:
a += 1
print(a)
else:
x = False
QTimer + QEventLoop:
class test2(QtWidgets.QMainWindow):
def __init__(self):
super (test2, self).__init__()
test2.setGeometry(self,20, 20, 450, 100)
self.label = QtWidgets.QLabel("Yay",self)
self.show()
loop = QtCore.QEventLoop()
QtCore.QTimer.singleShot(5*1000, loop.quit)
loop.exec_()
a=50
b=60
x=True
while x:
if a != b:
a += 1
print(a)
else:
x = False
QTimer:
class test2(QtWidgets.QMainWindow):
def __init__(self):
super (test2, self).__init__()
test2.setGeometry(self,20, 20, 450, 100)
self.label = QtWidgets.QLabel("Yay",self)
self.show()
QtCore.QTimer.singleShot(5*1000, self.foo)
def foo(self):
a=50
b=60
x=True
while x:
if a != b:
a += 1
print(a)
else:
x = False
As a general conclusion, the tasks executed in the thread of the GUI should consume a time less than 30ms, if it is greater than that it is recommended to execute it in another thread. In the case that each task within the loop fulfills the above but there are many iterations, a QTimer can be used to perform the tasks of the loop periodically.
answered Nov 21 '18 at 4:42
eyllanesceyllanesc
80.9k103259
80.9k103259
it worked. Thank you for the detailed explanation.
– Krish
Nov 21 '18 at 5:18
add a comment |
it worked. Thank you for the detailed explanation.
– Krish
Nov 21 '18 at 5:18
it worked. Thank you for the detailed explanation.
– Krish
Nov 21 '18 at 5:18
it worked. Thank you for the detailed explanation.
– Krish
Nov 21 '18 at 5:18
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53405246%2fpyqt5-child-window-is-not-displayed-until-while-loop-is-executed%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown