我正在尝试将我的PyQt4代码转换为PyQt5。
在PyQt4中,一个简单的width()和height()函数能够为我提供窗口的确切大小,但是在PyQt5中,它们分别为我提供默认值640和480。
这个PyQt4代码为我提供了正确的值。
from PyQt4 import QtCore, QtGui
import sys
class Window(QtGui.QWidget):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.showMaximized()
self.screen_width = self.width()
self.screen_height = self.height()
print(self.screen_width, self.screen_height)
if __name__ == '__main__':
app = QtGui.QApplication([])
window = Window()
sys.exit(app.exec_())
但是相同的功能给我的PyQt5中的值不正确。
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
class Window(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.showMaximized()
self.screen_width = self.width()
self.screen_height = self.height()
print(self.screen_width, self.screen_height)
if __name__ == '__main__':
app = QtWidgets.QApplication([])
window = Window()
sys.exit(app.exec_())