用户:wrmdcxy查看:1 回复:3 评论:1 创建时间:2020-03-03T10:15:15
【作品展示】

【作品介绍】
感谢@函数Notch 提供的灵感原网址https://shequ.codemao.cn/community/233790
我在TA基础上添加了功能
【作品源代码】
import easygui as g
import os,sys
class Logger(object):
def __init__(self):
self.terminal = sys.stdout
self.log = ''
def write(self, message):
self.terminal.write(message)
self.log += message
def flush(self):
pass
def __str__(self):
return self.log
def getExec(code):
sys.stdout = Logger()
file = sys.stdout
exec(str(code))
os.system('cls')
return str(file).replace('\n', '')
class PythonShell2(object):
def __init__(self,pmethod):
self.method=pmethod
g.msgbox('Welcome to use Python Shell2.0','Python Shell2.0')
g.msgbox('coding method:'+self.method,'Python Shell2.0')
def LongCoding(self):
choise=''
g.msgbox('You can code at the console','Python Shell2.0','coding')
print('\nYou can input "LCIOK" to continue\nYou can input "exit" to exit,too\nPlease code:')
while True:
oc=''
Fcode=input()
if Fcode=='exit':
sys.exit(0)
while Fcode!='LCIOK':
oc=oc+Fcode+'\n'
Fcode=input()
if Fcode=='exit':
sys.exit(0)
try:
print('=>',end='')
exec(oc)
except SyntaxError:
print('SyntaxError:invalid syntax')
except Exception as e:
e=repr(e).replace('("', ':')
e=e.replace('",)', '')
print(e)
def ImportFile(self):
ch=''
encoding='utf-8'
g.msgbox('File reading is supported only Text document(.txt)','Python Shell2.0')
while ch != 'Exit':
code=''
fpath=g.enterbox('The file path:', 'Python Shell2.0')
while True:
try:
with open(fpath,'r',encoding=encoding,errors='strict') as f:
for c in f:
code+=c
break
except FileNotFoundError:
ch = g.buttonbox('The file does not exist','Python Shell2.0',('Try again','Exit'))
if ch=='Exit':
sys.exit(0)
else:
fpath=g.enterbox('The file path:','Python Shell2.0')
except TypeError:
ch = g.buttonbox('Do you want to exit?','Python Shell2.0', ('No','Exit'))
if ch=='Exit':
sys.exit(0)
else:
fpath=g.enterbox('The file path:', 'Python Shell2.0')
except UnicodeDecodeError:
encoding = g.enterbox('Decoding failure.This file\'s Coding format is?(module:gbk).You can input"exit" to exit', 'Python Shell2.0')
if encoding == 'exit':
sys.exit(0)
else:
if g.ynbox('Do you want to change file path?','Python Shell2.0'):
fpath = g.enterbox('The file path:', 'Python Shell2.0')
if ch!='Exit':
try:
g.msgbox('The file is running.','Python Shell2.0')
exec(code)
ch = g.buttonbox('End of the run','Python Shell2.0', ('Again', 'Exit'))
except SyntaxError:
ch=g.buttonbox('SyntaxError:invalid syntax','Python Shell2.0',('Again', 'Exit'))
except Exception as e:
ch=g.buttonbox(e,'Python Shell2.0',('Again', 'Exit'))
def mainProgram(self):
ch=None
while ch!='Yes':
if self.method=='Normal coding':
user=PythonShell1()
user.Coding()
ch='Yes'
elif self.method=='Long coding':
self.LongCoding()
ch='Yes'
elif self.method=='Import file':
self.ImportFile()
ch='Yes'
class PythonShell1(object):
def Coding(self):
codeTab=''
choise=''
while choise!='Exit':
if codeTab=='':
code=input('>>>')
else:
code=input('...')
if code.endswith(':'):
codeTab+=code+'\n'
continue
else:
if codeTab=='':
try:
exec(code)
except SyntaxError:
print('SyntaxError:invalid syntax')
except Exception as e:
e=repr(e).replace('("', ':')
e=e.replace('",)', '')
print(e)
else:
try:
exec(codeTab+code)
codeTab = ''
except SyntaxError:
choise = print('SyntaxError:invalid syntax')
codeTab = ''
except Exception as e:
e = repr(e).replace('("', ':')
e = e.replace('",)', '')
print(e)
codeTab = ''
if code=='' and codeTab=='':
choise=input('Want to exit?You can input "exit" to exit or pressing enter to continue:')
if choise=='exit':
sys.exit(0)
else:
continue
else:
continue
def main_program(self):
os.system('cls')
print('Welcome to use Python Shell')
self.Coding()
class Programs(object):
def __init__(self,shell):
self.Shell=shell
print('Welcome to use this product')
def PythonShell20(self):
method=g.buttonbox('Please choose a coding method','Python Shell2.0',('Normal coding','Long coding','Import file','Change to Python Shell1.0','Exit'))
if method=='Exit':
sys.exit(0)
elif method=='Change to Python Shell1.0':
self.PythonShell10()
else:
User=PythonShell2(method)
User.mainProgram()
def PythonShell10(self):
user=PythonShell1()
Method=g.buttonbox('Please choose an action', 'Python Shell',('coding','Change to Python Shell2.0', 'Exit'))
if Method=='Exit':
sys.exit(0)
elif Method=='Change to Python Shell2.0':
self.PythonShell20()
else:
user.main_program()
def importantMainProgram(self):
if self.Shell=='Use Python Shell1.0':
self.PythonShell10()
else:
self.PythonShell20()
def mostImportantMainProgram():
she=g.buttonbox('Please choose an action', 'Python Shell',('Use Python Shell1.0','Use Python Shell2.0','Exit'))
if she=='Exit':
sys.exit(0)
else:
product=Programs(she)
product.importantMainProgram()
#以上功能定义完毕
#接下来调用主程序
#程序启动
mostImportantMainProgram()
【提示】
部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:
https://python.codemao.cn