这是我历经几个小时搞得,还行,至少可以像CMD那样输入了,效果图片:
·知识点:
1.控件套入Text控件。
2.tag_config以及insert的标记
3.exec和eval函数的用法。
·大家最喜欢的源码时间(Python3):
#引入一堆库
class tk:
from tkinter import Tk,Entry,Toplevel,Listbox
from tkinter.scrolledtext import ScrolledText
#设置信息,可选
class terminal_infos:
version='1.0'#版本
by='Takanawa-door'#作者
running_space={
'__name__':'__console__'}#运行空间(用于存储变量的)
exec('''def print(*value):
return None
def input(*value):
return None
def set(*value):
return None
def Back(*value):
pass
del input,print,set,Back''',running_space)#先把那些Python基础函数替换了
input_list=[]#这个是输入命令记载输入命令的列表
class os:
from os import getcwd,chdir,startfile,popen
from os.path import isfile,isdir
#新建函数以便将图标载入窗口中
def icon_for_window(tkwindow,filevalue,temofilename='tempicon.ico'):
try:
import base64
tmp = open(temofilename, "wb+")
tmp.write(base64.b64decode(filevalue))
tmp.close()
tkwindow.iconbitmap(temofilename)
from os import remove
remove(temofilename)
except:
pass
#运行输入的内容调用的函数
def run_command(command,terminal,commandinput):
def contiune_command():
terminal.insert('end','\n')
terminal.insert('end',f'\n{os.getcwd()}\n','green')
terminal.insert('end',f'$ ')
terminal.window_create('end',window=commandinput)
commandinput.focus_set()#"""
errortext=f'错误指令"{command.strip()}"。'
command=str(command)#这玩意是应付编辑器不知道command是什么类型的
terminal_infos.input_list.append(command)#增加输入了什么命令
terminal.config(state='n')#解锁terminal(Text)
terminal.delete('end')#删除输入控件
commandinput.delete(0,'end')#删除控件里输入的文本
if command.strip()=='':#如果啥也没输入
terminal.insert('end',command)#就复述输入内容
elif os.isfile(command.strip().replace('"','')) or os.isfile(os.getcwd()+command.strip().replace('"','')) or os.isfile(command.strip().replace('"','')+'.exe') or os.isfile(os.getcwd()+command.strip().replace('"','')+'.exe'):#如果是个文件
try:
os.startfile(command.strip().replace('"',''))
except:
try:
os.startfile(os.getcwd()+command.strip().replace('"',''))
except:
try:
os.startfile(command.strip().replace('"','')+'.exe')
except:
os.startfile(os.getcwd()+command.strip().replace('"','')+'.exe')
terminal.insert('end',command)
contiune_command()
elif len(command.strip())>=2:#当命令长度超过2时