我有此功能可将对象居中放在屏幕中间。
我想居中QMainWindow,QInputDialog和QMessageBox。
这是我的MessageBox:
def _Warning(self,_type):
infoBox = QtWidgets.QMessageBox()
infoBox.setIcon(QtWidgets.QMessageBox.Warning)
infoBox.setWindowTitle("Warning")
if (_type=="File"):
infoBox.setText("The File Already exist in the current Directory")
else:
infoBox.setText("The Folder Already exist in the current Directory")
self.center(infoBox)
infoBox.exec_()
这是我的QInputDialog:
def AddFile_B(self):
self.cuadro = QInputDialog()
self.center(self.cuadro)
text, okPressed = self.cuadro.getText(self, "New File","File Name:", QLineEdit.Normal, "")
if okPressed and text != '':
file = File_Node(text)
verify = self.bonsai_B.addChild(file)
if (verify == True):
item = QtWidgets.QListWidgetItem(None,0)
self.TreeB.addItem(item)
else:
del file
self._Warning("File")
这是我的中心功能:
def center(self,object):
qtRectangle = object.frameGeometry()
centerPoint = QtWidgets.QDesktopWidget().availableGeometry().center()
qtRectangle.moveCenter(centerPoint)
object.move(qtRectangle.topLeft())
我只能居中QMainWindow。
逻辑是将对象移动到topLeft点(screenWidth / 2-objectWidth / 2,screenHeight /
2-objectHeight / 2),但是我不知道自己在做什么。
在QMessageBox
此方法中调整尺寸的情况下exec_()
,因此可能的解决方案是QTimer.singleShot()
在显示后的瞬间使用几何形状进行更改。
from functools import partial
from PyQt5 import QtCore, QtWidgets
def center(window):
# https://wiki.qt.io/How_to_Center_a_Window_on_the_Screen
window.setGeometry(
QtWidgets.QStyle.alignedRect(
QtCore.Qt.LeftToRight,
QtCore.Qt.AlignCenter,
window.size(),
QtWidgets.qApp.desktop().availableGeometry(),
)
)
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.btn_warning = QtWidgets.QPushButton(
"Open QMessageBox", clicked=self.open_qmessagebox
)
lay = QtWidgets.QVBoxLayout(self)
lay.addWidget(self.btn_warning)
center(self)
@QtCore.pyqtSlot()
def open_qmessagebox(self):
infoBox = QtWidgets.QMessageBox()
infoBox.setIcon(QtWidgets.QMessageBox.Warning)
infoBox.setWindowTitle("Warning")
infoBox.setText("The XXX Already exist in the current Directory")
wrapper = partial(center, infoBox)
QtCore.QTimer.singleShot(0, wrapper)
infoBox.exec_()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
对于QInputDialog,QInputDialog :: getText()方法是静态的,因此“
self.cuadro”对象不是窗口,因为该窗口是在该方法中创建的。如果将父级传递给getText(),则默认情况下它将相对于此居中。
因此,如果QMainWindow居中并假定QMainWindow是自身,则无需修改任何内容。
相反,如果父母不在屏幕上居中,则有两种可能的解决方案:
不要使用静态方法并通过QInputDialog实例实现逻辑:
from functools import partial
from PyQt5 import QtCore, QtWidgets
def center(window):
# https://wiki.qt.io/How_to_Center_a_Window_on_the_Screen
window.setGeometry(
QtWidgets.QStyle.alignedRect(
QtCore.Qt.LeftToRight,
QtCore.Qt.AlignCenter,
window.size(),
QtWidgets.qApp.desktop().availableGeometry(),
)
)
class Widget(QtWidgets.QWidget):
def init(self, parent=None):
super().init(parent)
self.btn_inputdialog = QtWidgets.QPushButton(
"Open QInputDialog", clicked=self.open_qinputdialog
)
lay = QtWidgets.QVBoxLayout(self)
lay.addWidget(self.btn_inputdialog)
center(self)
@QtCore.pyqtSlot()
def open_qinputdialog(self):
dialog = QtWidgets.QInputDialog(self)
dialog.setWindowTitle("New File")
dialog.setLabelText("File Name:")
dialog.setTextEchoMode(QtWidgets.QLineEdit.Normal)
dialog.setTextValue("")
wrapper = partial(center, dialog)
QtCore.QTimer.singleShot(0, wrapper)
text, okPressed = (
dialog.textValue(),
dialog.exec_() == QtWidgets.QDialog.Accepted,
)
if okPressed and text:
print(text)
if name == “main”:
import sys
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
继续使用静态方法,并使用findChildren()获取窗口
from functools import partial
from PyQt5 import QtCore, QtWidgets
def center(window):
# https://wiki.qt.io/How_to_Center_a_Window_on_the_Screen
window.setGeometry(
QtWidgets.QStyle.alignedRect(
QtCore.Qt.LeftToRight,
QtCore.Qt.AlignCenter,
window.size(),
QtWidgets.qApp.desktop().availableGeometry(),
)
)
class Widget(QtWidgets.QWidget):
def init(self, parent=None):
super().init(parent)
self.btn_inputdialog = QtWidgets.QPushButton(
"Open QInputDialog", clicked=self.open_qinputdialog
)
lay = QtWidgets.QVBoxLayout(self)
lay.addWidget(self.btn_inputdialog)
center(self)
@QtCore.pyqtSlot()
def open_qinputdialog(self):
parent = self
dialogs = parent.findChildren(QtWidgets.QInputDialog)
def onTimeout():
dialog, *_ = set(parent.findChildren(QtWidgets.QInputDialog)) - set(dialogs)
center(dialog)
QtCore.QTimer.singleShot(0, onTimeout)
text, okPressed = QtWidgets.QInputDialog.getText(
parent, "New File", "File Name:", QtWidgets.QLineEdit.Normal, ""
)
if okPressed and text:
print(text)
if name == “main”:
import sys
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
问题内容: 如何使用jQuery在屏幕中央设置a ? 问题答案: 我喜欢向jQuery添加功能,因此该功能将有所帮助: 现在我们可以这样写:
问题内容: 我是.Net开发人员,但出于某种原因,我不知为何要用java创建一个简单的应用程序。我能够创建该应用程序,但是我的问题是启动应用程序时如何在屏幕上居中放置表单? 这是我的代码: 上面的代码工作正常,但问题是我已经看到表格从最左上角移到中间屏幕。我还尝试在事件中添加该代码,但仍显示相同的操作。有更好的方法吗?就像里面有一个。还是如果上面的代码正确,我将在哪个事件上放? 感谢您阅读本文。
问题内容: 嗨,我正在使用类似于以下内容的方法来将div定位在屏幕中间: 但是,这样做的问题是它将项目放置在页面的中间而不是屏幕的中间。因此,如果页面高了几个屏幕,而我使div出现时,我就位于页面的顶部(该部分的顶部显示在屏幕上)。您必须向下滚动才能查看它。 有人可以告诉我您如何将其显示在屏幕中间吗? 问题答案: 只需添加即可,即使您向下滚动也可以看到它。
问题内容: 如何将通过javascript 函数打开的弹出窗口居中显示在屏幕变量中心,以当前选定的屏幕分辨率为中心? 问题答案: 更新:它现在也可以在尚未超出屏幕宽度和高度的窗口上运行! 如果您使用双显示器,则窗口将水平居中,而不是垂直居中…使用此功能可以解决此问题。 用法示例:
在 一到三个 。有没有办法在后始终将单元格放置在屏幕底部?