码迷,mamicode.com
首页 > Windows程序 > 详细

【PyQt5 学习记录】005:QMainWindow 及状态栏、菜单栏和工具栏

时间:2018-10-22 10:24:44      阅读:260      评论:0      收藏:0      [点我收藏+]

标签:tla   col   argv   title   bubuko   居中   记录   pyqt5   ...   

  1 #!/usr/bin/env python
  2 
  3 import sys
  4 from PyQt5.QtWidgets import (QApplication,
  5                              QMainWindow,
  6                              QWidget,
  7                              QAction,
  8                              QLabel,
  9                              QTextEdit,
 10                              QLineEdit,
 11                              QPushButton,
 12                              QGridLayout)
 13 from PyQt5.QtCore import Qt
 14 from PyQt5.QtGui import QIcon
 15 
 16 
 17 class MainWindow(QMainWindow):
 18     def __init__(self, parent=None):
 19         super(MainWindow, self).__init__(parent, Qt.Window)
 20 
 21         # 创建一个菜单栏:
 22         bar_menu = self.menuBar()
 23         # 为菜单栏添加文件菜单
 24         menu_file = bar_menu.addMenu(文件(&F))
 25         # 为菜单栏添加编辑菜单
 26         menu_edit = bar_menu.addMenu(编辑(&E))
 27 
 28         # 添加一个动作:
 29         action_file = QAction(打开, self)
 30         # 为动作添加快捷建:
 31         # 值得注意的是在 MAC os 中 Ctrl 指的是 command 。
 32         action_file.setShortcut(Ctrl+O)
 33         # 为动作添加图标:
 34         action_file.setIcon(QIcon(open.png))
 35         # 将点击动作的信号连接到 action_open 方法:
 36         action_file.triggered.connect(self.action_open)
 37         # 将打开动作添加到文件菜单中:
 38         menu_file.addAction(action_file)
 39 
 40         action_copy = QAction(复制, self)
 41         action_copy.setIcon(QIcon(copy.png))
 42         # 在 MAC os 中 Meta 才是 Ctrl 按钮:
 43         action_copy.setShortcut(Meta+C)
 44         action_copy.triggered.connect(self.action_copy)
 45         menu_edit.addAction(action_copy)
 46 
 47         # 创建一个工具栏:
 48         bar_tool = self.addToolBar(工具栏)
 49         # 为工具栏添加按钮:
 50         bar_tool.addAction(action_file)
 51         # 为添加分割线:
 52         bar_tool.addSeparator()
 53         bar_tool.addAction(action_copy)
 54 
 55         label_1 = QLabel(居右)
 56         # 设置Label的文字为居右并垂直居中:
 57         label_1.setAlignment(Qt.AlignRight | Qt.AlignVCenter)
 58         label_2 = QLabel(宽度因子为 1)
 59         label_3 = QLabel(居中)
 60         # 设置Label的文字为居中并垂直居中:
 61         label_3.setAlignment(Qt.AlignCenter | Qt.AlignVCenter)
 62         self.label_4 = QLabel(Label_4)
 63 
 64         # 创建一个状态栏:
 65         status = self.statusBar()
 66         # 在状态栏显示信息:
 67         # 注意:显示信息时,组件不会显示
 68         # status.showMessage("Ready!")
 69         # 为状态栏添加一个stretch(拉伸因子)为1的Label
 70         status.addWidget(label_1, 1)
 71         status.addWidget(label_2, 1)
 72         status.addWidget(label_3, 3)
 73         status.addWidget(self.label_4, 1)
 74 
 75         """
 76         为 QMainWindow 添加其他组件的方法:
 77         1. 将组件添加到一个布局中;
 78         2. 创建一个 QWidget 设置为 1 中的布局;
 79         3. 将 2 中的 QWidget 设为central widget 。
 80         """
 81 
 82         line_edit = QLineEdit()
 83         # 为 line_edit 设置灰色提示文字:
 84         line_edit.setPlaceholderText(请输入...)
 85 
 86         push_button = QPushButton()
 87         push_button.setText(确认)
 88 
 89         text_edit = QTextEdit()
 90         text_edit.setPlaceholderText("编辑结果...")
 91 
 92         # 创建一个网格布局:
 93         layout_grid = QGridLayout()
 94         layout_grid.addWidget(line_edit, 0, 0)
 95         layout_grid.addWidget(push_button, 0, 1)
 96         layout_grid.addWidget(text_edit, 1, 0, 2, 2)
 97 
 98         # 创建一个 QWidget ,并将其布局设置为 layout_grid :
 99         widget = QWidget()
100         widget.setLayout(layout_grid)
101         # 将 widget 设为主窗口的 central widget :
102         self.setCentralWidget(widget)
103 
104         # 设置窗口大小:
105         self.resize(500, 500)
106         # 设置窗口标题:
107         self.setWindowTitle(u"QMainWindow")
108         # 显示窗口:
109         self.show()
110 
111     def action_open(self):
112         self.label_4.setText("按下了打开按钮!")
113 
114     def action_copy(self):
115         self.label_4.setText("按下了复制按钮!")
116 
117 
118 if __name__ == "__main__":
119     app = QApplication(sys.argv)
120     window = MainWindow()
121     sys.exit(app.exec_())

效果如下图:

技术分享图片

 

【PyQt5 学习记录】005:QMainWindow 及状态栏、菜单栏和工具栏

标签:tla   col   argv   title   bubuko   居中   记录   pyqt5   ...   

原文地址:https://www.cnblogs.com/jmtm/p/9828414.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!