Python Forum
Troubble creating loops in PyQt
Thread Rating:
  • 2 Vote(s) - 2.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Troubble creating loops in PyQt
#1
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need help with creating dynamic columns with for loops for stock prices PaDat 2 895 Feb-22-2023, 04:34 AM
Last Post: PaDat
  Trouble creating loops Den 3 2,304 Oct-23-2019, 05:59 PM
Last Post: ibreeden

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020