猫史档案馆


Python逐字输出教程

用户:JUST_workshop官方JUST_workshop官方查看:14 回复:2 评论:14 创建时间:2022-12-24T21:57:37


第一次发教程啊。。。好激动

emotion_编程猫_紧张

好的,废话不多说,直接进入正题

先看一下简洁版:

import random 
import sys 
import time 

def printf(s): 
    for c in s + '\n': 
     sys.stdout.write(c) 
     sys.stdout.flush() # defeat buffering 
     time.sleep(random.random() * 0.1) 

再来看一下本人的优化制作方式:

def replace(strname, *w):
    for x in w:
        strname = strname.replace(x[0], x[1])
    return strname


def print(*w, t=0.05, end="\n", sepr=" "):
    import sys
    import time
    str_all = ""

    for x in w:

        if w.index(x) == len(w)-1:
            str_all += str(x)
        else:
            str_all += str(x)+sepr
    str_all = replace(str_all, ("\\黑", "\033[30m"),
                      ("\\红", "\033[31m"),
                      ("\\绿", "\033[32m"),
                      ("\\黄", "\033[33m"),
                      ("\\蓝", "\033[34m"),
                      ("\\紫", "\033[35m"),
                      ("\\青", "\033[36m"),
                      ("\\白", "\033[37m"),
                      ("\\b红", "\033[41m"),
                      ("\\b绿", "\033[42m"),
                      ("\\b黄", "\033[43m"),
                      ("\\b蓝", "\033[44m"),
                      ("\\b紫", "\033[45m"),
                      ("\\b青""\033[46m"),
                      ("\\b白", "\033[47m"),
                      ("\\b黑", "\033[40m"),
                      ("\\", "\033[0m"))

    for y in str_all:
        sys.stdout.write(y)
        sys.stdout.flush()
        #print(z,end="")
        time.sleep(t)
    sys.stdout.write(end)

 

然后再来升及一下代码,使代码更加优质:

def print_lines(*w, t=0.05, line_end="\n", sepr="", line_t=0.1):
    import sys
    import time
    for x in w:
        if w.index(x) == len(w)-1:
            print(str(x), t=t, end=line_end)
        else:
            print(str(x), t=t, end=line_end)
            sys.stdout.write(sepr)
            time.sleep(line_t)

 

接着再讲一下最重要的清屏:

def clear():
    import sys
    sys.stdout.write("\033[2J\033[00H")

 

然后将使用注释给一下大家:

"""
   前景色            背景色            颜色
   --------------------------------------------
     30                40              黑色
     31                41              红色
     32                42              绿色
     33                43              黃色
     34                44              蓝色
     35                45              紫色
     36                46              青色
     37                47              白色
     90                100             亮黑
     91                101             亮红
     92                102             亮绿
     93                103             亮黃
     94                104             亮蓝
     95                105             亮紫
     96                106             亮青
     97                107             亮白
     
   显示方式         清除代码           意义
   --------------------------------------------
      0                --              终端默认设置
      4                24              使用下划线
      7                27              反白显示
      8                28              不可见
    
    
    光标设置 格式:\\033[数字+字母,数字、字母如下所示
    
    字母              数字             意义
   --------------------------------------------
     A                 x               光标上移x行
     B                 x               光标下移x行
     C                 x               光标左移x列
     D                 x               光标右移x列
     H                 y,x             光标设置为(x,y)(注意顺序)
     K                 无              清除从光标到行尾的内容
     s                 无              保存光标位置
     u                 无              回复光标位置
     l                 ?25             隐藏光标
     h                 ?25             显示光标
     J                 2               清屏(但不会恢复光标)
"""

 

emmm...感觉有亿点氵

那在结尾给一下整体代码:

"""
   前景色            背景色            颜色
   --------------------------------------------
     30                40              黑色
     31                41              红色
     32                42              绿色
     33                43              黃色
     34                44              蓝色
     35                45              紫色
     36                46              青色
     37                47              白色
     90                100             亮黑
     91                101             亮红
     92                102             亮绿
     93                103             亮黃
     94                104             亮蓝
     95                105             亮紫
     96                106             亮青
     97                107             亮白
     
   显示方式         清除代码           意义
   --------------------------------------------
      0                --              终端默认设置
      4                24              使用下划线
      7                27              反白显示
      8                28              不可见
    
    
    光标设置 格式:\\033[数字+字母,数字、字母如下所示
    
    字母              数字             意义
   --------------------------------------------
     A                 x               光标上移x行
     B                 x               光标下移x行
     C                 x               光标左移x列
     D                 x               光标右移x列
     H                 y,x             光标设置为(x,y)(注意顺序)
     K                 无              清除从光标到行尾的内容
     s                 无              保存光标位置
     u                 无              回复光标位置
     l                 ?25             隐藏光标
     h                 ?25             显示光标
     J                 2               清屏(但不会恢复光标)
"""


def replace(strname, *w):
    for x in w:
        strname = strname.replace(x[0], x[1])
    return strname


def print(*w, t=0.05, end="\n", sepr=" "):
    import sys
    import time
    str_all = ""

    for x in w:

        if w.index(x) == len(w)-1:
            str_all += str(x)
        else:
            str_all += str(x)+sepr
    str_all = replace(str_all, ("\\黑", "\033[30m"),
                      ("\\红", "\033[31m"),
                      ("\\绿", "\033[32m"),
                      ("\\黄", "\033[33m"),
                      ("\\蓝", "\033[34m"),
                      ("\\紫", "\033[35m"),
                      ("\\青", "\033[36m"),
                      ("\\白", "\033[37m"),
                      ("\\b红", "\033[41m"),
                      ("\\b绿", "\033[42m"),
                      ("\\b黄", "\033[43m"),
                      ("\\b蓝", "\033[44m"),
                      ("\\b紫", "\033[45m"),
                      ("\\b青""\033[46m"),
                      ("\\b白", "\033[47m"),
                      ("\\b黑", "\033[40m"),
                      ("\\", "\033[0m"))

    for y in str_all:
        sys.stdout.write(y)
        sys.stdout.flush()
        #print(z,end="")
        time.sleep(t)
    sys.stdout.write(end)


def print_lines(*w, t=0.05, line_end="\n", sepr="", line_t=0.1):
    import sys
    import time
    for x in w:
        if w.index(x) == len(w)-1:
            print(str(x), t=t, end=line_end)
        else:
            print(str(x), t=t, end=line_end)
            sys.stdout.write(sepr)
            time.sleep(line_t)


def show_img(url, prin="正在显示文件(没看见的去任务栏找)", inpu=" 看/运行 完点【ENTER】键:"):
    print(prin)
    import os
    os.system(url)
    input(inpu)


def clear():
    import sys
    sys.stdout.write("\033[2J\033[00H")

 

本人不善言表代码逻辑,大家自己理一理吧。

希望对大家有用。。。

#代码解释一切#


回复

上一页1 页 / 共 1下一页
听雨_可乐听雨_可乐

没看懂……虽然我也学PY

点赞0


评论


JUST_workshop官方JUST_workshop官方

'''主文件:'''

import about_file as f
from tkinter import *
import webbrowser as webo


class commands():
    def __init__(self):
        self.file_name = f.getFileName('main_code_html.html')

    def save(self):
        with open(self.file_name, 'w') as file:
            global codes
            file.write(codes.get("1.0", END))

    def run(self):
        self.save()
        webo.open('file://'+self.file_name)

    def get(self):
        with open(self.file_name, 'r') as file:
            code = file.read()
            if code == '':
                return ''
            else:
                return code


tk = Tk()
c = commands()

tk.title('Python写的H喵L编译器')
tk.geometry('500x300+200+200')

btn1 = Button(tk, text='保存', command=c.save)
btn1.grid(row=0, column=0)

btn2 = Button(tk, text='运行', command=c.run)
btn2.grid(row=0, column=1)

codes = Text(tk)
codes.place(x=0, y=25)
codes.insert(END, c.get())

tk.mainloop()



import os
import platform


def getFileName(name="record.txt"):
    tmp_path = ""
    if platform.system() == "Darwin":
        tmp_path = os.path.expanduser("~/学而思直播/code/cache/asset_pool")
    else:
        tmp_path = os.path.expanduser(r"~\学而思直播\code\cache\asset_pool")
    whole_path = os.path.join(tmp_path, name)
    #初始化
    if not os.path.exists(whole_path):
        with open(whole_path, "w", encoding="utf-8") as f:
            f.write("0")
    return whole_path


def openFile(data_path):
    if platform.system() == "Windows":
        os.startfile(data_path)
    elif platform.system() == "Darwin":
        subprocess.Popen(["open", data_path])
    else:
        subprocess.Popen(["xdg-open", data_path])

点赞0


评论