Advertisement
Guest User

Untitled

a guest
Aug 24th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. import json
  2. import sys
  3. from PyQt5.QtWidgets import (QWidget, QLabel, QLineEdit, QVBoxLayout,
  4. QTextEdit, QGridLayout, QApplication,QPushButton)
  5. from PyQt5 import QtCore
  6. from PyQt5.QtCore import Qt
  7. from prompt_toolkit import prompt
  8. import subprocess
  9.  
  10. class Example(QWidget):
  11.  
  12. def __init__(self):
  13. super().__init__()
  14. self.pageNumber = 0
  15. self.initUI()
  16.  
  17.  
  18. def initUI(self):
  19.  
  20. """
  21. Layout :-
  22. |Title - QLineEdit|
  23.  
  24. |Content - QEditText|
  25.  
  26. |Confirm - QPushButton| --> calls writeToJson to add data to json file
  27. """
  28.  
  29. layout = QVBoxLayout()
  30.  
  31. closeBtn = QPushButton("X",self)
  32. closeBtn.setFixedWidth(30)
  33. closeBtn.setStyleSheet("color:black")
  34. closeBtn.clicked.connect(lambda :QApplication.instance().quit())
  35. layout.addWidget(closeBtn,Qt.AlignRight)
  36.  
  37.  
  38. self.titleEditText = QLineEdit()
  39. layout.addWidget(self.titleEditText)
  40. self.titleEditText.setStyleSheet("QLineEdit {margin-left:0px; margin-right:0px;border: 2px solid red;}")
  41. self.titleEditText.setPlaceholderText("Title")
  42.  
  43.  
  44. self.contentEditText = QTextEdit()
  45. self.contentEditText.setPlaceholderText("Content")
  46. self.contentEditText.setStyleSheet("QTextEdit {margin-left:0px; margin-right:0px;border: 2px solid red;}")
  47. layout.addWidget(self.contentEditText)
  48.  
  49. self.confirmBtn = QPushButton("Confirm")
  50. self.confirmBtn.clicked.connect(self.writeToJson)
  51. self.confirmBtn.setFixedWidth(60)
  52. layout.addWidget(self.confirmBtn)
  53.  
  54. self.commandText = QLineEdit()
  55. self.commandText.returnPressed.connect(self.runCmd)
  56. self.commandText.setPlaceholderText("Enter cmd command and Hit Enter!")
  57. self.commandText.setStyleSheet("QLineEdit {padding:20px;margin-top:40px;margin-left:0px; margin-right:0px;border: 4px dotted blue;}")
  58. layout.addWidget(self.commandText)
  59.  
  60. self.setWindowFlag(Qt.FramelessWindowHint)
  61. self.setStyleSheet("background: #FFF;")
  62. self.setLayout(layout)
  63. self.setGeometry(300, 300, 800, 500)
  64. self.setWindowTitle('cmd')
  65. self.show()
  66.  
  67. def runCmd(self):
  68. commad = self.commandText.displayText()
  69. ret = subprocess.getoutput(commad)
  70. print(ret)
  71. self.contentEditText.setText(ret)
  72.  
  73.  
  74. def writeToJson(self):
  75. import json
  76.  
  77.  
  78. with open("myNotes.json",'r+') as file:
  79. pre_json = self.getPreData() #fetch existing data for appending pages into it
  80.  
  81. json_text = { "title":self.titleEditText.displayText(),
  82. "content":self.contentEditText.toPlainText()}
  83.  
  84. pre_json["page-" + str(self.pageNumber)] = json_text #append new page
  85.  
  86. self.pageNumber +=1
  87.  
  88. print(pre_json)
  89.  
  90. file.seek(0)
  91. json.dump(pre_json,file)
  92.  
  93. def getPreData(self):
  94. """
  95. This will fetch existing json data in json file
  96. """
  97. import json
  98. with open("myNotes.json") as file:
  99. json_data = file.readlines()
  100. file.seek(0)
  101. if(json_data != []):
  102. return json.load(file)
  103. return {}
  104.  
  105.  
  106.  
  107.  
  108. def mousePressEvent(self,event):
  109. if event.button() == Qt.LeftButton:
  110. self.moving = True
  111. self.offset = event.pos()
  112.  
  113. def mouseMoveEvent(self,event):
  114. if self.moving: self.move(event.globalPos()-self.offset)
  115.  
  116.  
  117. if __name__ == '__main__':
  118.  
  119.  
  120. app = QApplication(sys.argv)
  121. ex = Example()
  122. # sys.stdout = ex
  123.  
  124. print("hello")
  125. sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement