4 窗口的居中,控件居中满屏显示,和tooltip
1)主窗口如何居中显示?是用下面四步完成
winRectangle = self.frameGeometry() #这是得到窗口矩形
centerPoint =QDesktopWidget().availableGeometry().center() #获取屏幕的中心点坐标。
winRectangle.moveCenter(centerPoint) #将窗口同型矩形winRectangle,移动到屏幕中央
self.move(winRectangle.topLeft()) #将窗口移动到那个同型矩形winRectangle中
2 如何让控件居窗口的中部?
textEdit = QTextEdit() 假如控件是个编辑框,
self.setCentralWidget(textEdit) 窗口让此编辑框居于窗口中间
注意:该用法很容易发生窗口的空间被一个控件占满,而其它控件没有空间,正确用法需要和QLayout类搭配使用,后文将有详细描述。
3 如何让窗内控件拥有提示功能?
infoLabel = QLabel() 假如控件是个提示框,
self.setToolTip('This is a tooltip message.')鼠标临于该提示框,将出现'This is a tooltip message.'的提示
实验代码:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# .Data:.2019/2/4
from PyQt5.QtWidgetsimport QMainWindow,QApplication,QWidget, \
QPushButton,QLabel,QDesktopWidget,QTextEdit
import sys
class MyWnd( QMainWindow ):
def __init__(self):
super().__init__()
self.title ="this is first"
self.top =100;self.left =100;self.width =400;self.height =300
closebutton = QPushButton("Close",self)
closebutton.setGeometry(20,20,80,30)
closebutton.clicked.connect(self.close )
closebutton.setToolTip('This is a close button.')
pushbutton = QPushButton("Push",self)
pushbutton.setGeometry(100,20,80,30)
pushbutton.clicked.connect(self.buttonpushfunction )
pushbutton.setToolTip('This is a push button.')
self.m_lab = QLabel(self)
self.m_lab.setGeometry(20,80,80,30)
# self.setCentralWidget(self.m_lab)
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.top ,self.left ,self.width ,self.height )
winRectangle =self.frameGeometry()
centerPoint = QDesktopWidget().availableGeometry().center()
winRectangle.moveCenter(centerPoint)
self.move(winRectangle.topLeft())
self.show()
def buttonpushfunction(self):
self.m_lab.setText("pushed button")
if __name__ =="__main__":
app = QApplication( sys.argv )
win = MyWnd()
sys.exit( app.exec() )
网友评论