第一个PyQt项目——简易记事本UI

import sys
from PyQt5.QtWidgets import QMainWindow, QAction, QMenu, QApplication,QPushButton,QDesktopWidget,QMessageBox,qApp
from PyQt5.QtGui import QIcon
import json

class Notebook(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):

menubar=self.menuBar()
with open('menu.json',encoding='utf-8') as f:
all_menu=json.load(f)
for i in all_menu:
menu_1=menubar.addMenu(i)
for j in all_menu[i]:
menu_2=QAction(QIcon(j['icon']),j['name'],self)
if j.get('shortcut'):
menu_2.setShortcut(j['shortcut'])
menu_2.setStatusTip(j['tip'])
if j['tri']:
menu_2.triggered.connect(j['tri'])
menu_1.addAction(menu_2)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
note = Notebook()
sys.exit(app.exec_())

#json代码(实现子菜单内容)
{
"文件(F)": [
{
"icon": "new.jpg",
"name": "新建(N)",
"shortcut": "Ctrl+N",
"tip": "新建文件",
"tri": null
},
{
"icon": "new.jpg",
"name": "打开(O)",
"shortcut": "Ctrl+O",
"tip": "打开文件",
"tri": null
},
{
"icon": "save.jpg",
"name": "保存(S)",
"shortcut": "Ctrl+S",
"tip": "保存文件",
"tri": null
},
{
"icon": "saveas.jpg",
"name": "另存为(A)",
"shortcut": "Ctrl+Shift+S",
"tip": "将文件另存为...",
"tri": null
},
{
"icon": "set.jpg",
"name": "页面设置(U)",
"shortcut": null,
"tip": "设置页面",
"tri": null
},
{
"icon": "dayin.jpg",
"name": "打印(P)",
"shortcut":"Ctrl+P",
"tip": "打印页面",
"tri": null
},
{
"icon": "exit.jpg",
"name": "退出(X)",
"shortcut": null,
"tip": "退出页面",
"tri": null
}],
"编辑":[
{
"icon": "return.jpg",
"name": "撤销(U)",
"shortcut": "Ctrl+Z",
"tip": "撤销",
"tri": null
},
{
"icon": "cut.jpg",
"name": "剪切(T)",
"shortcut": "Ctrl+X",
"tip": "剪切",
"tri": null
},
{
"icon": "copy.jpg",
"name": "复制(C)",
"shortcut": "Ctrl+C",
"tip": "复制",
"tri": null
},
{
"icon": "paspe.jpg",
"name": "粘贴(U)",
"shortcut": "Ctrl+V",
"tip": "粘贴",
"tri": null
},
{
"icon": "del.jpg",
"name": "删除(L)",
"shortcut": "Del",
"tip": "删除",
"tri": null
},
{
"icon": "find.jpg",
"name": "查找(F)",
"shortcut": "Ctrl+F",
"tip": "查找指定内容",
"tri": null
},
{
"icon": "next.jpg",
"name": "查找下一个(N)",
"shortcut": "F3",
"tip": "查找下一个",
"tri": null
},
{
"icon": "replace.jpg",
"name": "替换(R)",
"shortcut": "Ctrl+H",
"tip": "将已有内容替换掉",
"tri": null
},
{
"icon": "get.jpg",
"name": "转到(G)",
"shortcut": "Ctrl+G",
"tip": "跳转到下一处",
"tri": null
},
{
"icon": "all.jpg",
"name": "全选(A)",
"shortcut": "Ctrl+A",
"tip": "选择全部内容",
"tri": null
},
{
"icon": "time.jpg",
"name": "时间/日期(D)",
"shortcut": "F5",
"tip": "显示当前时间",
"tri": null
} ],
"格式(O)": [
{
"icon": "line.jpg",
"name": "自动换行(W)",
"shortcut": null,
"tip": "自动换行",
"tri": null }, { "icon": "font.jpg", "name": "字体(F)", "shortcut": null, "tip": "设置字体", "tri": null }], "查看(V)": [ { "icon": "satuation.jpg", "name": "状态栏(S)", "shortcut": null, "tip": "显示或关闭状态栏", "tri": null }], "帮助(H)": [ { "icon": "vhelp.jpg", "name": "查看帮助(H)", "shortcut": null, "tip": "显示帮助手册", "tri": null }, { "icon": "about.jpg", "name": "关于记事本(A)", "shortcut": null, "tip": "了解更多...", "tri": null }] }
 

转载于:https://www.cnblogs.com/Ann0609/p/9643603.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值