-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.py
More file actions
105 lines (83 loc) · 3.5 KB
/
Copy pathindex.py
File metadata and controls
105 lines (83 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import sys
import os
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget, QPlainTextEdit, QPushButton, QHBoxLayout, QFileDialog, QTextEdit
class IDE(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("PyQt5 Python IDE")
self.setGeometry(100, 100, 800, 600)
self.initUI()
def initUI(self):
# Create main widget
main_widget = QWidget(self)
self.setCentralWidget(main_widget)
# Set the layout for the main widget
layout = QVBoxLayout()
# Code Editor area (PlainTextEdit for the code input)
self.code_editor = QPlainTextEdit(self)
self.code_editor.setPlaceholderText("Write your Python code here...")
layout.addWidget(self.code_editor)
# Output area (TextEdit for displaying execution results)
self.output_area = QTextEdit(self)
self.output_area.setReadOnly(True) # Make the output area read-only
layout.addWidget(self.output_area)
# Button to execute code
self.run_button = QPushButton("Run Code", self)
self.run_button.clicked.connect(self.run_code)
layout.addWidget(self.run_button)
# File management buttons (Open, Save)
file_layout = QHBoxLayout()
self.open_button = QPushButton("Open File", self)
self.save_button = QPushButton("Save File", self)
self.open_button.clicked.connect(self.open_file)
self.save_button.clicked.connect(self.save_file)
file_layout.addWidget(self.open_button)
file_layout.addWidget(self.save_button)
layout.addLayout(file_layout)
# Set the main layout of the window
main_widget.setLayout(layout)
def run_code(self):
# Get the code from the code editor
code = self.code_editor.toPlainText()
try:
# Redirect output to the output area
output = self.execute_code(code)
self.output_area.setText(output)
except Exception as e:
self.output_area.setText(f"Error: {str(e)}")
def execute_code(self, code):
try:
# Redirect standard output and error to capture print statements and errors
from io import StringIO
import sys
old_stdout = sys.stdout
old_stderr = sys.stderr
sys.stdout = sys.stderr = StringIO()
# Execute the code
exec(code)
# Get the output
output = sys.stdout.getvalue()
# Restore original stdout and stderr
sys.stdout = old_stdout
sys.stderr = old_stderr
return output if output else "Execution complete, no output."
except Exception as e:
return str(e)
def open_file(self):
file_name, _ = QFileDialog.getOpenFileName(self, "Open Python File", "", "Python Files (*.py);;All Files (*)")
if file_name:
with open(file_name, 'r') as file:
code = file.read()
self.code_editor.setPlainText(code)
def save_file(self):
file_name, _ = QFileDialog.getSaveFileName(self, "Save Python File", "", "Python Files (*.py);;All Files (*)")
if file_name:
with open(file_name, 'w') as file:
code = self.code_editor.toPlainText()
file.write(code)
if __name__ == "__main__":
app = QApplication(sys.argv)
ide = IDE()
ide.show()
sys.exit(app.exec_())