Last active
March 27, 2022 23:19
-
-
Save eccstartup/efb98af84616006981ba8b1527eaf1ba to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PyQt6.QtWidgets import QApplication, QLabel, QTextEdit, QVBoxLayout, QWidget, QPushButton | |
import sys | |
import ast | |
def multiline_eval(expr, context={}): | |
"Evaluate several lines of input, returning the result of the last line" | |
tree = ast.parse(expr) | |
eval_exprs = [] | |
exec_exprs = [] | |
for module in tree.body: | |
if isinstance(module, ast.Expr): | |
eval_exprs.append(module.value) | |
else: | |
exec_exprs.append(module) | |
exec_expr = ast.Module(exec_exprs, type_ignores=[]) | |
exec(compile(exec_expr, 'file', 'exec'), context) | |
results = [] | |
for eval_expr in eval_exprs: | |
results.append(eval(compile(ast.Expression((eval_expr)), 'file', 'eval'), context)) | |
return '\n'.join([str(r) for r in results]) | |
app = QApplication(sys.argv) | |
window = QWidget() | |
window.setWindowTitle('Calculator') | |
layout = QVBoxLayout() | |
expr = QTextEdit('') | |
result = QLabel('') | |
def btn_clicked(): | |
text = expr.toPlainText() | |
try: | |
res = multiline_eval(text) | |
except Exception as e: | |
print(e) | |
res = 'Wrong input!' | |
result.setText(str(res)) | |
btn = QPushButton('calc') | |
layout.addWidget(expr) | |
layout.addWidget(result) | |
layout.addWidget(btn) | |
window.setLayout(layout) | |
btn.clicked.connect(btn_clicked) | |
window.show() | |
sys.exit(app.exec()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PyQt6.QtWidgets import QApplication, QLabel, QTextEdit, QVBoxLayout, QWidget, QPushButton | |
import sys | |
import ast | |
import math | |
from io import StringIO | |
def multiline_eval(expr, context={}): | |
"Evaluate several lines of input, returning the result of the last line" | |
tree = ast.parse(expr) | |
eval_exprs = [] | |
exec_exprs = [] | |
sys.stdout = new_stdout = StringIO() | |
for module in tree.body: | |
if isinstance(module, ast.Expr): | |
eval_exprs.append(module.value) | |
else: | |
exec_exprs.append(module) | |
exec_expr = ast.Module(exec_exprs, type_ignores=[]) | |
exec(compile(exec_expr, 'file', 'exec'), context) | |
results = [] | |
for eval_expr in eval_exprs: | |
res = str(eval(compile(ast.Expression((eval_expr)), 'file', 'eval'), context)) | |
size = math.ceil(len(res) / 170) | |
for j in range(size-1): | |
results.append(res[j*170:(j+1)*170]) | |
results.append(res[(size-1)*170:]) | |
sysout = new_stdout.getvalue() | |
return '\n'.join(results), sysout | |
app = QApplication(sys.argv) | |
window = QWidget() | |
window.setWindowTitle('Calculator') | |
layout = QVBoxLayout() | |
expr = QTextEdit('') | |
result = QLabel('') | |
result.setMaximumWidth(1200) | |
def btn_clicked(): | |
text = expr.toPlainText() | |
try: | |
res, sysout = multiline_eval(text) | |
if len(res) == 0: | |
res = '<No output>' | |
if sysout != '': | |
res += '\nsys.stdout:\n' + sysout | |
except Exception as e: | |
print(e) | |
res = '<Wrong input>' | |
result.setText(str(res)) | |
btn = QPushButton('calc') | |
btn_exit = QPushButton('exit') | |
layout.addWidget(expr) | |
layout.addWidget(result) | |
layout.addWidget(btn) | |
layout.addWidget(btn_exit) | |
window.setLayout(layout) | |
btn.clicked.connect(btn_clicked) | |
btn_exit.clicked.connect(lambda: sys.exit()) | |
window.show() | |
sys.exit(app.exec()) |
Author
eccstartup
commented
Mar 27, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment