PyQt5:第四天
为窗口添加菜单和工具栏
QTDesigner会默认给主窗口创建一个菜单栏(最上方)和一个状态栏(最下方),可以右击点击“添加工具栏”在菜单栏的下方添加一个工具栏。
菜单栏中的菜单项可以直接输入添加,而工具栏中的选项则需要通过动作编辑器来添加。
python代码创建主窗口
QT有三种窗口:QMainWindow、QWidget、QDialog。
QMainWindow:可以包含菜单栏、状态栏、工具栏、标题栏,最常见。
QDialog:是对话窗口的基类,弹出一个对话框,用于执行短期任务,没有菜单栏、状态栏、工具栏。
QWidget:不确定窗口的用途,就是用QWidget。
import sys
from PyQt5.QtWidgets import QMainWindow,QApplication
from PyQt5.QtGui import QIcon
class FirstMainWin(QMainWindow):
def __init__(self):
super(FirstMainWin, self).__init__()
# 标题
self.setWindowTitle('第一个主窗口应用')
# 设置尺寸
self.resize(400,300)
# 状态栏
self.status = self.statusBar()
self.status.showMessage('只存在5秒的消息', 5000)
if __name__ == '__main__':
app = QApplication(sys.argv)
# 图标
app.setWindowIcon(QIcon('D:/PycharmProjects/pro2/th.ico'))
main = FirstMainWin()
main.show()
sys.exit(app.exec_())
主窗口居中
# QDesktopWidget
import sys
from PyQt5.QtWidgets import QMainWindow,QApplication,QDesktopWidget
from PyQt5.QtGui import QIcon
class centerForm(QMainWindow):
def __init__(self):
super(centerForm, self).__init__()
# 标题
self.setWindowTitle('让窗口居中')
# 设置尺寸
self.resize(400,300)
def center(self):
# 屏幕坐标
screen = QDesktopWidget().screenGeometry()
# 窗口坐标
size = self.geometry()
newLeft = (screen.width() - size.width()) / 2
newTop = (screen.height() - size.height()) / 2
self.move(newLeft, newTop)
if __name__ == '__main__':
app = QApplication(sys.argv)
main = centerForm()
main.show()
sys.exit(app.exec_())
退出应用程序
import sys
from PyQt5.QtWidgets import QMainWindow,QApplication,QHBoxLayout,QWidget,QPushButton
class QuitApplication(QMainWindow):
def __init__(self):
super(QuitApplication, self).__init__()
self.resize(300,120)
self.setWindowTitle('退出应用程序')
# 添加button、绑定方法
self.button1 = QPushButton('退出应用程序')
self.button1.clicked.connect(self.onClick_Button)
# 水平布局并添加组件
layout = QHBoxLayout()
layout.addWidget(self.button1)
# 放置所有的组件
mainFrame = QWidget()
mainFrame.setLayout(layout)
# 主框架放在整个窗口上
self.setCentralWidget(mainFrame)
def onClick_Button(self):
sender = self.sender()
# print(sender.windowIconText() + '按钮被按下')
app = QApplication.instance()
app.quit()
if __name__ == '__main__':
app = QApplication(sys.argv)
main = QuitApplication()
main.show()
sys.exit(app.exec_())
屏幕坐标系
import sys
from PyQt5.QtWidgets import QMainWindow,QApplication,QHBoxLayout,QWidget,QPushButton
def onClick_Button():
print("1:窗口左上角、工作区尺寸")
print("widget.x() = %d" % widget.x())
print("widget.y() = %d" % widget.y())
print("widget.width() = %d" % widget.width())
print("widget.height() = %d" % widget.height())
print("2:工作区左上角、工作区尺寸")
print("widget.geometry().x() = %d" % widget.geometry().x())
print("widget.geometry().y() = %d" % widget.geometry().y())
print("widget.geometry().width() = %d" % widget.geometry().width())
print("widget.geometry().height() = %d" % widget.geometry().height())
print("3:窗口左上角、窗口尺寸")
print("widget.frameGeometry().x() = %d" % widget.frameGeometry().x())
print("widget.frameGeometry().y() = %d" % widget.frameGeometry().y())
print("widget.frameGeometry().width() = %d" % widget.frameGeometry().width())
print("widget.frameGeometry().height() = %d" % widget.frameGeometry().height())
app = QApplication(sys.argv)
widget = QWidget()
btn = QPushButton(widget)
btn.setText('按钮')
btn.move(24,52)
btn.clicked.connect(onClick_Button)
widget.resize(300,240)
widget.move(250,200)
widget.setWindowTitle('屏幕坐标系')
widget.show()
sys.exit(app.exec_())