Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import json
- import sys
- from PyQt5.QtWidgets import (QWidget, QLabel, QLineEdit, QVBoxLayout,
- QTextEdit, QGridLayout, QApplication,QPushButton)
- from PyQt5 import QtCore
- from PyQt5.QtCore import Qt
- from prompt_toolkit import prompt
- import subprocess
- class Example(QWidget):
- def __init__(self):
- super().__init__()
- self.pageNumber = 0
- self.initUI()
- def initUI(self):
- """
- Layout :-
- |Title - QLineEdit|
- |Content - QEditText|
- |Confirm - QPushButton| --> calls writeToJson to add data to json file
- """
- layout = QVBoxLayout()
- closeBtn = QPushButton("X",self)
- closeBtn.setFixedWidth(30)
- closeBtn.setStyleSheet("color:black")
- closeBtn.clicked.connect(lambda :QApplication.instance().quit())
- layout.addWidget(closeBtn,Qt.AlignRight)
- self.titleEditText = QLineEdit()
- layout.addWidget(self.titleEditText)
- self.titleEditText.setStyleSheet("QLineEdit {margin-left:0px; margin-right:0px;border: 2px solid red;}")
- self.titleEditText.setPlaceholderText("Title")
- self.contentEditText = QTextEdit()
- self.contentEditText.setPlaceholderText("Content")
- self.contentEditText.setStyleSheet("QTextEdit {margin-left:0px; margin-right:0px;border: 2px solid red;}")
- layout.addWidget(self.contentEditText)
- self.confirmBtn = QPushButton("Confirm")
- self.confirmBtn.clicked.connect(self.writeToJson)
- self.confirmBtn.setFixedWidth(60)
- layout.addWidget(self.confirmBtn)
- self.commandText = QLineEdit()
- self.commandText.returnPressed.connect(self.runCmd)
- self.commandText.setPlaceholderText("Enter cmd command and Hit Enter!")
- self.commandText.setStyleSheet("QLineEdit {padding:20px;margin-top:40px;margin-left:0px; margin-right:0px;border: 4px dotted blue;}")
- layout.addWidget(self.commandText)
- self.setWindowFlag(Qt.FramelessWindowHint)
- self.setStyleSheet("background: #FFF;")
- self.setLayout(layout)
- self.setGeometry(300, 300, 800, 500)
- self.setWindowTitle('cmd')
- self.show()
- def runCmd(self):
- commad = self.commandText.displayText()
- ret = subprocess.getoutput(commad)
- print(ret)
- self.contentEditText.setText(ret)
- def writeToJson(self):
- import json
- with open("myNotes.json",'r+') as file:
- pre_json = self.getPreData() #fetch existing data for appending pages into it
- json_text = { "title":self.titleEditText.displayText(),
- "content":self.contentEditText.toPlainText()}
- pre_json["page-" + str(self.pageNumber)] = json_text #append new page
- self.pageNumber +=1
- print(pre_json)
- file.seek(0)
- json.dump(pre_json,file)
- def getPreData(self):
- """
- This will fetch existing json data in json file
- """
- import json
- with open("myNotes.json") as file:
- json_data = file.readlines()
- file.seek(0)
- if(json_data != []):
- return json.load(file)
- return {}
- def mousePressEvent(self,event):
- if event.button() == Qt.LeftButton:
- self.moving = True
- self.offset = event.pos()
- def mouseMoveEvent(self,event):
- if self.moving: self.move(event.globalPos()-self.offset)
- if __name__ == '__main__':
- app = QApplication(sys.argv)
- ex = Example()
- # sys.stdout = ex
- print("hello")
- sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement