【Python扩展】使用Tkinter做一个类似CMD的终端

这是我历经几个小时搞得,还行,至少可以像CMD那样输入了,效果图片:
效果图.png


·知识点:
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时
        if command
  • 12
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 12
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值