Python 为什么加载UI后我的窗口大小会改变?
我正在尝试制作一种类似谷歌恐龙的游戏。问题是,当我启动我的应用程序时,我的窗口的高度会在几秒钟后发生变化,因此当我试图将“恐龙”放在底部时,它仍然是浮动的,因为窗口的高度发生了变化 “恐龙”是漂浮的盒子: 这是主窗口Python 为什么加载UI后我的窗口大小会改变?,python,pyqt,pyqt5,Python,Pyqt,Pyqt5,我正在尝试制作一种类似谷歌恐龙的游戏。问题是,当我启动我的应用程序时,我的窗口的高度会在几秒钟后发生变化,因此当我试图将“恐龙”放在底部时,它仍然是浮动的,因为窗口的高度发生了变化 “恐龙”是漂浮的盒子: 这是主窗口 import sys from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QVBoxLayout, QFrame from PyQt5.QtCore import Qt, QTimer, QRect f
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QVBoxLayout, QFrame
from PyQt5.QtCore import Qt, QTimer, QRect
from PyQt5.QtGui import QPen, QBrush, QPainter
from PyQt5 import uic
import random
class animation(QWidget):
def __init__(self):
super().__init__()
self.shapes = []
self.dimension = 50
#hilo del juego
self.game_timer = QTimer(self)
self.game_timer.timeout.connect(self.animation)
self.game_timer.start(10)
#crear las figuras
self.shapes_generator_timer = QTimer(self)
self.shapes_generator_timer.timeout.connect(self.new_shape)
self.shapes_generator_timer.start(500)
#jugador
coord_x = self.width()
coord_y = self.height() - self.dimension
self.player = QRect(200, coord_y-2, self.dimension, self.dimension)
def new_shape(self):
coord_x = self.width()
coord_y = self.height() - self.dimension
rect = QRect(coord_x, coord_y-2, self.dimension, self.dimension)
self.shapes.append(rect)
def animation(self):
for index,shape in enumerate(self.shapes):
shape.moveLeft(shape.left() - 5)
self.delete_shape(index,shape)
self.update()
print(self.height())
def delete_shape(self, index,shape):
if shape.getCoords()[2] < 0:
del self.shapes[index]
print(len(self.shapes))
def paintEvent(self, event):
painter = QPainter(self)
painter.setPen(QPen(Qt.white,0,Qt.SolidLine))
painter.setBrush(QBrush(Qt.white,Qt.SolidPattern))
painter.drawRect(self.rect())
painter.setPen(QPen(Qt.black,1,Qt.SolidLine))
painter.setBrush(QBrush(Qt.green,Qt.SolidPattern))
for shape in self.shapes:
painter.drawRect(shape)
painter.drawRect(self.player)
ui, _ = uic.loadUiType("PYQT5\interface-animation.ui")
class MyApp(QWidget,ui):
def __init__(self):
super().__init__()
self.setupUi(self)
self.animation = animation()
frame = self.findChild(QFrame, "frame")
layout = QHBoxLayout()
layout.addWidget(self.animation)
frame.setLayout(layout)
app = QApplication(sys.argv)
myApp = MyApp()
myApp.show()
sys.exit(app.exec_())
导入系统
从PyQt5.qtwidts导入QApplication、QWidget、QHBoxLayout、QVBoxLayout、QFrame
从PyQt5.QtCore导入Qt、QTimer、QRect
从PyQt5.QtGui导入QPen、QBrush、QPainter
从PyQt5导入uic
随机输入
类动画(QWidget):
定义初始化(自):
super()。\uuuu init\uuuuu()
self.shapes=[]
自我尺寸=50
#朱伊戈山
self.game\u timer=QTimer(self)
self.game\u timer.timeout.connect(self.animation)
自我游戏计时器启动(10)
#克雷尔·拉斯福拉斯酒店
self.shapes\u generator\u timer=QTimer(self)
self.shapes\u generator\u timer.timeout.connect(self.new\u shape)
自动形状发生器定时器启动(500)
#胡加多
coord_x=self.width()
坐标y=self.height()-self.dimension
self.player=QRect(200,坐标y-2,self.dimension,self.dimension)
def新_形状(自):
coord_x=self.width()
坐标y=self.height()-self.dimension
rect=QRect(坐标x,坐标y-2,自维,自维)
self.shapes.append(rect)
def动画(自):
对于索引,枚举中的形状(self.shapes):
shape.moveLeft(shape.left()-5)
self.delete_形状(索引,形状)
self.update()
打印(self.height())
def delete_形状(自、索引、形状):
如果shape.getCoords()[2]<0:
del self.shapes[索引]
打印(透镜(自形状))
def paintEvent(自身,事件):
油漆工=油漆工(自身)
画师设置笔(QPen(Qt.white,0,Qt.SolidLine))
画师.镶嵌(QBrush(Qt.白色,Qt.立体图案))
painter.drawRect(self.rect())
画师设置笔(QPen(Qt.黑色,1,Qt.实线))
画师.镶嵌(QBrush(Qt.绿色,Qt.立体图案))
对于self.shapes中的形状:
画师:drawRect(形状)
painter.drawRect(self.player)
ui,qt5=uic.loadUiType(“PYQT5\interface animation.ui”)
类MyApp(QWidget,ui):
定义初始化(自):
super()。\uuuu init\uuuuu()
self.setupUi(self)
self.animation=animation()
frame=self.findChild(QFrame,“frame”)
布局=QHBoxLayout()
layout.addWidget(self.animation)
frame.setLayout(布局)
app=QApplication(sys.argv)
myApp=myApp()
myApp.show()
sys.exit(app.exec_())
这是我的UI文件
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form</class>
<widget class="QWidget" name="Form">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1201</width>
<height>638</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QFrame" name="frame">
<property name="styleSheet">
<string notr="true">background-color:rgb(0,0,0);</string>
</property>
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
形式
0
0
1201
638
形式
背景色:rgb(0,0,0);
QFrame::StyledPanel
QFrame::提出
除非指定固定大小,否则不能依赖小部件实例化中的几何图形:默认情况下,所有QWidget类在初始化期间的大小都为640x480
在您的情况下,解决方案很简单:在中移动播放器位置的计算,这是每次调整小部件大小时(由系统、用户或任何父/子布局要求)接收的事件:
请注意,您的实现并不完全正确,因为如果在播放时调整窗口大小,障碍物的位置将错误,因此我建议另一种方法:始终在y=0处创建项目,并在绘制事件中平移矩形:
class animation(QWidget):
def __init__(self):
# ...
self.player = QRect(200, 0, self.dimension, self.dimension)
def new_shape(self):
coord_x = self.width()
rect = QRect(coord_x, 0, self.dimension, self.dimension)
self.shapes.append(rect)
# ...
def paintEvent(self, event):
painter = QPainter(self)
painter.setPen(QPen(Qt.white,0,Qt.SolidLine))
painter.setBrush(QBrush(Qt.white,Qt.SolidPattern))
painter.drawRect(self.rect())
coord_y = self.height() - self.dimension
painter.setPen(QPen(Qt.black,1,Qt.SolidLine))
painter.setBrush(QBrush(Qt.green,Qt.SolidPattern))
for shape in self.shapes:
painter.drawRect(shape.translated(0, coord_y))
painter.drawRect(self.player.translated(0, coord_y))
你可能在一些在线教程中发现了这种方法,不幸的是,他们在这方面完全错了,并暗示这是一种非常糟糕的做法
class animation(QWidget):
def __init__(self):
# ...
self.player = QRect(200, 0, self.dimension, self.dimension)
def new_shape(self):
coord_x = self.width()
rect = QRect(coord_x, 0, self.dimension, self.dimension)
self.shapes.append(rect)
# ...
def paintEvent(self, event):
painter = QPainter(self)
painter.setPen(QPen(Qt.white,0,Qt.SolidLine))
painter.setBrush(QBrush(Qt.white,Qt.SolidPattern))
painter.drawRect(self.rect())
coord_y = self.height() - self.dimension
painter.setPen(QPen(Qt.black,1,Qt.SolidLine))
painter.setBrush(QBrush(Qt.green,Qt.SolidPattern))
for shape in self.shapes:
painter.drawRect(shape.translated(0, coord_y))
painter.drawRect(self.player.translated(0, coord_y))
class MyApp(QWidget,ui):
def __init__(self):
# ...
self.frame.setLayout(layout)