Mar-02-2017, 08:05 PM
Below is two examples of code. They are both the same except one has a pygame window open in it.
from PyQt4 import QtCore, QtGui
import sys
class Main:
def setup(self, MainWindow):
MainWindow.resize(647, 498)
self.b4 = QtGui.QPushButton(MainWindow)
self.b4.setGeometry(QtCore.QRect(0, 120, 75, 23))
self.b4.clicked.connect(self.move)
def move(self):
l.canmove = True
l.move()
class loop(QtCore.QThread):
def __init__(self):
self.canmove = False
def move(self):
while self.canmove:
print('spam')
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
l = loop()
ui = Main()
ui.setup(MainWindow)
MainWindow.show()
sys.exit(app.exec_())from PyQt4 import QtCore, QtGui
import sys
import pygame
class Main:
def setup(self, MainWindow):
MainWindow.resize(647, 498)
self.b4 = QtGui.QPushButton(MainWindow)
self.b4.setGeometry(QtCore.QRect(0, 120, 75, 23))
self.b4.clicked.connect(self.move)
def move(self):
l.canmove = True
l.move()
pygame.display.set_mode([300,300])
class loop(QtCore.QThread):
def __init__(self):
self.canmove = False
def move(self):
while self.canmove:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pass
print('spam')
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
l = loop()
ui = Main()
ui.setup(MainWindow)
MainWindow.show()
sys.exit(app.exec_())What I do not understand is the fact that in the first code example when the loop starts the qt window crashes, bet in the next example with the pygame code in it the loop works and nothing crashes. Why is that? What is pygame doing that keeps the qt window from crashing, and how can I properly create a loop with qt that will work without pygame? I do not understand what is going on here except I get the idea that I am not using the QThread correctly, but I don't see how having the pygame code is able the make it work.
