-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHP Python IDE.py
More file actions
100 lines (80 loc) · 2.4 KB
/
Copy pathHP Python IDE.py
File metadata and controls
100 lines (80 loc) · 2.4 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
# HP Python IDE™
# کتابخانهها
import os
import sys
import tempfile
import subprocess
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
class HPPythonIDE(App):
# عنوان پنجره
title = "HP Python IDE™"
def build(self):
# لایهٔ اصلی
root = BoxLayout(
orientation="vertical",
spacing=5,
padding=5
)
# نوار ابزار
toolbar = BoxLayout(
size_hint_y=0.1,
spacing=5
)
# دکمهٔ اجرا
run_button = Button(text="Run")
run_button.bind(on_press=self.run_code)
toolbar.add_widget(run_button)
# ویرایشگر کد
self.editor = TextInput(
text='print("Hello, World!")',
multiline=True
)
# بخش نمایش خروجی
self.output = TextInput(
readonly=True,
multiline=True,
size_hint_y=0.3
)
# افزودن بخشها به پنجره
root.add_widget(toolbar)
root.add_widget(self.editor)
root.add_widget(self.output)
return root
def run_code(self, *args):
# دریافت کد نوشتهشده
code = self.editor.text
# ساخت فایل موقت
with tempfile.NamedTemporaryFile(
delete=False,
suffix=".py",
mode="w",
encoding="utf-8"
) as file:
file.write(code)
filename = file.name
try:
# اجرای کد با همان مفسر پایتون
result = subprocess.run(
[sys.executable, filename],
capture_output=True,
text=True
)
# نمایش خروجی یا خطا
output = result.stdout + result.stderr
if output.strip():
self.output.text = output
else:
self.output.text = "Program finished successfully."
except Exception as error:
# نمایش خطای احتمالی
self.output.text = str(error)
finally:
# حذف فایل موقت
try:
os.remove(filename)
except OSError:
pass
HPPythonIDE().run()