如何解决如何使用PyQt将窗口放在左上角?
我试图通过运行以下代码将窗口置于左上角:
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)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。