问题描述
import sys
from PyQt5 import QtGui
class MainWindow(QtGui.QWidget):
def __init__(self):
super(MainWindow,self).__init__()
self.initUI()
def initUI(self):
self.resize(640,480)
self.topLeft()
self.show()
def topLeft()(self):
frameGm = self.frameGeometry()
topLeftPoint = QApplication.desktop().availableGeometry().topLeft()
frameGm.movetopLeft(topLeftPoint)
self.move(frameGm.topLeft())
def main():
app = QApplication(sys.argv)
mainWindow = MainWindow()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
但是这样做,我的窗口和屏幕的左侧之间出现了缝隙,如下所示:
您知道它的来源以及如何预防吗?
解决方法
发生这种情况的原因是,当第一次在屏幕上显示窗口时,show()
和窗口实际 映射到地图的那一刻之间发生了一些事情(很多事情)。屏幕。
为了实现所需的功能,您需要将移动延迟一定的时间(通常,在事件循环的“周期”上就足够了)。通常,使用具有1个超时的单次QTimer即可。
class MainWindow(QtGui.QWidget):
def __init__(self):
super(MainWindow,self).__init__()
self.initUI()
def initUI(self):
self.resize(640,480)
QTimer.singleShot(1,self.topLeft)
self.show()
def topLeft(self):
# no need to move the point of the geometry rect if you're going to use
# the reference top left only
topLeftPoint = QApplication.desktop().availableGeometry().topLeft()
self.move(topLeftPoint)