Python PyQt中的PyGame刷新?
我看到了一个类似的话题,这似乎正是我想要的。然而,在添加了一个菜单栏和其他一些项目之后,我意识到我无法刷新pygame的显示。我认为这是因为他们的方式是,它只是一个pygame表面,而不是pygame显示器,所以做pygame.display.update/flip不起作用。我想知道是否有办法做到这一点 我唯一能想到的不是把pygame放在PyQT里,而是把PyQT放在pygame里,但我真的不知道怎么做Python PyQt中的PyGame刷新?,python,user-interface,pygame,pyqt5,Python,User Interface,Pygame,Pyqt5,我看到了一个类似的话题,这似乎正是我想要的。然而,在添加了一个菜单栏和其他一些项目之后,我意识到我无法刷新pygame的显示。我认为这是因为他们的方式是,它只是一个pygame表面,而不是pygame显示器,所以做pygame.display.update/flip不起作用。我想知道是否有办法做到这一点 我唯一能想到的不是把pygame放在PyQT里,而是把PyQT放在pygame里,但我真的不知道怎么做 from PyQt5 import QtGui from PyQt5.QtWidgets
from PyQt5 import QtGui
from PyQt5.QtWidgets import QWidget, QDesktopWidget, QApplication, QMainWindow, QStatusBar
import pygame
import sys
from pygame.locals import *
#get GUi settings
windowWidth = 500
windowHeight = 500
#colors
white = (255,255,255)
red = (255,0,0)
green = (0,255,0)
black = (0,0,0)
yellow = (240,240,0)
class ImageWidget(QWidget):
def __init__(self,surface,parent=None):
super(ImageWidget,self).__init__(parent)
w=surface.get_width()
h=surface.get_height()
self.data=surface.get_buffer().raw
self.image=QtGui.QImage(self.data,w,h,QtGui.QImage.Format_RGB32)
def paintEvent(self,event):
qp=QtGui.QPainter()
qp.begin(self)
qp.drawImage(0,0,self.image)
qp.end()
class MainWindow(QMainWindow):
global windowWidth
global windowHeight
def __init__(self,surface,parent=None):
global windowWidth
global windowHeight
super(MainWindow,self).__init__(parent)
self.setGeometry(500, 100, windowWidth, windowHeight)
print(windowWidth)
self.move(QApplication.desktop().screen().rect().center() - self.rect().center())
self.setCentralWidget(ImageWidget(surface))
self.showMaximized()
#self.showFullScreen()
#screen_resolution = app.desktop().screenGeometry()
#windowWidth, windowHeight = screen_resolution.width(), screen_resolution.height()
self.setGeometry(0, 0, windowWidth, windowHeight)
self.createUI()
def createUI(self):
self.setWindowTitle('Test game')
#Menubar
#First item
menu = self.menuBar().addMenu('Menu')
menuSettings = menu.addAction('Settings')
#action.triggered.connect(self.changeFilePath)
menuNewGame = menu.addAction('New Game')
menuSaveGame = menu.addAction('Save Game')
menuLoadGame = menu.addAction('Load Game')
# Statusbar
#self.statusBar().showMessage("test",5000)
self.statusBar().showMessage("Game Loaded")
def resizePyGame():
gameScreen.fill(green)
pygame.display.update()
pygame.draw.circle(gameScreen, (255, 0, 255, 255), (100, 100), 50)
mainWindow.setGeometry(0, 0, windowWidth, windowHeight)
# pygame.display.set_mode((640,480), FULLSCREEN)
pygame.init()
#gameDisplay = pygame.display.set_mode((400,400),0,32)
gameScreen =pygame.Surface((200,100))
gameScreen.fill(red)
pygame.draw.circle(gameScreen,(255,255,255,255),(100,100),50)
app=QApplication(sys.argv)
mainWindow=MainWindow(gameScreen)
infoObject = pygame.display.Info()
windowWidth = infoObject.current_w
windowHeight = infoObject.current_h
#pygame.display.set_mode((50,100), FULLSCREEN)
resizePyGame()
mainWindow.show()
app.exec_()
您必须更改ImageWidget的image属性中存储的QImage;可能会添加如下更新方法:
def update(self, surface)
self.data=surface.get_buffer().raw
self.image=QtGui.QImage(self.data,w,h,QtGui.QImage.Format_RGB32)
或者将曲面存储在属性中前面答案中的示例:
import sys
from PyQt5 import QtWidgets
from PyQt5.QtCore import QTimer
from PyQt5.QtGui import QImage, QPainter
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
import pygame
import sys
class ImageWidget(QWidget):
def __init__(self,surface,parent=None):
super(ImageWidget,self).__init__(parent)
self.w=surface.get_width()
self.h=surface.get_height()
self.data=surface.get_buffer().raw
self.image=QImage(self.data,self.w,self.h,QImage.Format_RGB32)
#sloth
def update(self, surface):
self.data = surface.get_buffer().raw
self.image = QImage(self.data, self.w, self.h, QImage.Format_RGB32)
def paintEvent(self,event):
qp=QPainter()
qp.begin(self)
qp.drawImage(0,0,self.image)
qp.end()
class MainWindow(QtWidgets.QMainWindow):
def __init__(self,surface,parent=None):
super(MainWindow,self).__init__(parent)
self.setFixedWidth(900)
self.setFixedHeight(900)
self.surface=surface
self.img=ImageWidget(self.surface)
self.setCentralWidget(self.img)
self.button = QPushButton('PyQt5 button', self)
self.button.setToolTip('This is an example button')
self.button.move(800, 70)
self.button.clicked.connect(self.hagoAlgo)
#self.timer = QTimer()
#self.timer.timeout.connect(self.hagoAlgo)
#self.startTimer()
self.mu=0
def upd(self):
s = pygame.Surface((640, 480))
s.fill((64, 128, 192, 224))
pygame.draw.circle(s, (55, 11, 55, 111), (self.mu, self.mu), 50)
self.img.update(s)
#self.img = ImageWidget(self.surface)
def hagoAlgo(self):
self.mu += 1
print(self.mu)
self.upd()
self.update()
def startTimer(self):
self.timer.start(1000)
def endTimer(self):
self.timer.stop()
if __name__ == '__main__':
pygame.init()
s = pygame.Surface((640, 480))
s.fill((64, 128, 192, 224))
pygame.draw.circle(s, (255, 255, 255, 255), (100, 100), 50)
app = QApplication(sys.argv)
w = MainWindow(s)
w.show()
app.exec_()