Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在屏幕上居中显示QMessageBox和QInputDialog?_Python_Pyqt_Pyqt5_Qmessagebox_Qinputdialog - Fatal编程技术网

Python 如何在屏幕上居中显示QMessageBox和QInputDialog?

Python 如何在屏幕上居中显示QMessageBox和QInputDialog?,python,pyqt,pyqt5,qmessagebox,qinputdialog,Python,Pyqt,Pyqt5,Qmessagebox,Qinputdialog,我有这个函数把一个对象放在屏幕中间。 我想将QMainWindow、QInputDialog和QMessageBox放在中间 这是我的邮箱: def\u警告(自我,类型): infoBox=qtwidts.QMessageBox() setIcon(qtwidts.QMessageBox.Warning) infoBox.setWindowTitle(“警告”) 如果(_type==“文件”): infoBox.setText(“文件已存在于当前目录中”) 其他: infoBox.setText

我有这个函数把一个对象放在屏幕中间。

我想将QMainWindow、QInputDialog和QMessageBox放在中间

这是我的邮箱:

def\u警告(自我,类型):
infoBox=qtwidts.QMessageBox()
setIcon(qtwidts.QMessageBox.Warning)
infoBox.setWindowTitle(“警告”)
如果(_type==“文件”):
infoBox.setText(“文件已存在于当前目录中”)
其他:
infoBox.setText(“当前目录中已存在该文件夹”)
自我中心(信息盒)
infoBox.exec()
这是我的对话:

def AddFile_B(自):
self.cuadro=QInputDialog()
self.center(self.cuadro)
text,okPressed=self.cuadro.getText(self,“新文件”,“文件名:”,QLineEdit.Normal“”)
如果按下OK并显示文字!='':
文件=文件\节点(文本)
验证=self.bonsai_B.addChild(文件)
如果(验证==True):
item=QtWidgets.QListWidgetItem(无,0)
自我树附加项(项目)
其他:
del文件
自我警告(“文件”)
这是我的中心功能:

def中心(自身,对象):
qtRectangle=object.frameGeometry()
centerPoint=QtWidgets.QDesktopWidget().availableGeometry().center()
qtRectangle.moveCenter(中心点)
object.move(qtRectangle.topLeft())
我只能将qmain窗口居中

逻辑是将对象移动到左上角点(screenWidth/2-objectWidth/2,screenHeight/2-objectHeight/2),但我不知道我做错了什么。

-QMessageBox 在
QMessageBox
的情况下,这是在
exec!

从functools导入部分
从PyQt5导入QtCore、QtWidgets
def中心(窗口):
# https://wiki.qt.io/How_to_Center_a_Window_on_the_Screen
window.setGeometry(
qtwidts.QStyle.alignedRect(
QtCore.Qt.LeftToRight,
QtCore.Qt.AlignCenter,
window.size(),
qtwidts.qApp.desktop().availableGeometry(),
)
)
类小部件(qtwidts.QWidget):
def uuu init uuu(self,parent=None):
super()。\uuuu init\uuuu(父级)
self.btn_warning=qtwidts.QPushButton(
“打开QMessageBox”,单击=self.Open\u QMessageBox
)
lay=qtwidts.QVBoxLayout(self)
lay.addWidget(self.btn_警告)
中心(自我)
@QtCore.pyqtSlot()
def open_qmessagebox(自):
infoBox=qtwidts.QMessageBox()
setIcon(qtwidts.QMessageBox.Warning)
infoBox.setWindowTitle(“警告”)
infoBox.setText(“当前目录中已存在XXX”)
包装=部分(中间,信息框)
QtCore.QTimer.singleShot(0,包装器)
infoBox.exec()
如果名称=“\uuuuu main\uuuuuuuu”:
导入系统
app=qtwidts.QApplication(sys.argv)
w=Widget()
w、 show()
sys.exit(app.exec_())

-QInputDialog 对于QInputDialog,QInputDialog::getText()方法是静态的,因此“self.cuadro”对象不是窗口,因为窗口是在该方法中创建的。如果将父对象传递给getText(),则默认情况下它将相对于该对象居中

因此,如果QMainWindow居中,并且假设QMainWindow是self,则无需修改任何内容

相反,如果父级未在屏幕上居中,则有两种可能的解决方案:

  • 不要使用静态方法并通过QInputDialog实例实现逻辑:
从functools导入部分
从PyQt5导入QtCore、QtWidgets
def中心(窗口):
# https://wiki.qt.io/How_to_Center_a_Window_on_the_Screen
window.setGeometry(
qtwidts.QStyle.alignedRect(
QtCore.Qt.LeftToRight,
QtCore.Qt.AlignCenter,
window.size(),
qtwidts.qApp.desktop().availableGeometry(),
)
)
类小部件(qtwidts.QWidget):
def uuu init uuu(self,parent=None):
super()。\uuuu init\uuuu(父级)
self.btn_inputdialog=qtwidts.QPushButton(
“打开QInputDialog”,单击=self.Open\u QInputDialog
)
lay=qtwidts.QVBoxLayout(self)
lay.addWidget(self.btn\u输入对话框)
中心(自我)
@QtCore.pyqtSlot()
def open_QInput对话框(自身):
dialog=QtWidgets.QInputDialog(self)
dialog.setWindowTitle(“新文件”)
setLabelText(“文件名:”)
setTextEchoMode(qtwidts.QLineEdit.Normal)
dialog.setTextValue(“”)
包装=部分(中间,对话框)
QtCore.QTimer.singleShot(0,包装器)
文本,OK=(
dialog.textValue(),
dialog.exec_uz()==qtwidts.QDialog.Accepted,
)
如果按下OK键并输入文字:
打印(文本)
如果名称=“\uuuuu main\uuuuuuuu”:
导入系统
app=qtwidts.QApplication(sys.argv)
w=Widget()
w、 show()
sys.exit(app.exec_())
  • 继续使用static方法并使用findChildren()获取窗口
从functools导入部分
从PyQt5导入QtCore、QtWidgets
def中心(窗口):
# https://wiki.qt.io/How_to_Center_a_Window_on_the_Screen
window.setGeometry(
qtwidts.QStyle.alignedRect(
QtCore.Qt.LeftToRight,
QtCore.Qt.AlignCenter,
window.size(),
qtwidts.qApp.desktop().availableGeometry(),
)
)
类小部件(qtwidts.QWidget):
def uuu init uuu(self,parent=None):
super()。\uuuu init\uuuu(父级)
self.btn_inputdialog=qtwidts.QPushButton(
“打开QInputDialog”,单击=self.Open\u QInputDialog
)
lay=qtwidts.QVBoxLayout(self)
放置