代码行数计数器

做了个记录代码函数的小程序,后缀名记得设置为.pyw,如果你装了python的话可以直接拿来用,免费自取。
功能说明:
1.记录总行数、当前行数、目标行数三个值

2.具有进度条功能

3.行数的多少能激发不同的反馈,如great,good等

4.具有段位系统,参考游戏段位设计

文件目录:

两个文件一个是源代码,一个txt文件保存代码总行数

源代码如下:

import os
import tkinter as tk
from tkinter import ttk, messagebox

# 定义目标行数
TARGET_LINES = 1000000

# 记录文件路径
record_file = "line_count_record.txt"

# 层次名称和颜色映射
tier_names = ["青铜", "白银", "黄金", "铂金", "钻石", "星耀", "王者", "最强王者", "荣耀王者", "归一"]
tier_colors = ["#CD7F32", "#C0C0C0", "#FFD700", "#38B0DE", "#CC3299", "#FF7F00", "#BC1717", "#FF7F00", "#FF2400", "#000000"]

def read_record():
    """读取记录文件中的行数信息"""
    if os.path.exists(record_file):
        with open(record_file, "r", encoding="utf-8") as f:
            data = f.readlines()
            if len(data) > 0:
                total_lines = int(data[0].strip())
                return total_lines
    return 0

def write_record(total_lines):
    """将行数信息写入记录文件"""
    with open(record_file, "w", encoding="utf-8") as f:
        f.write(f"{total_lines}\n")

def update_progress_bar():
    """更新进度条和百分比标签"""
    progress = (total_lines / TARGET_LINES) * 100
    progress_var.set(progress)
    label_percentage.config(text=f"{progress:.2f}%")

def get_tier_and_rank(total_lines):
    """根据总行数计算层次和等级"""
    tier_index = total_lines // 100000  # 每个层次代表10万行代码
    rank = (total_lines % 100000) // 10000 + 1  # 每个层次分10阶,每1万行表示1阶
    if tier_index >= len(tier_names):
        tier_index = len(tier_names) - 1  # 限制最大层次
        rank = 10  # 限制最大等级为10阶
    return tier_index, rank

def show_feedback(new_current_lines):
    """根据输入的行数显示反馈消息"""
    if new_current_lines > 1000:
        label_feedback.config(text="oh my god!", fg="red")
    elif new_current_lines > 500:
        label_feedback.config(text="unbelievable!", fg="purple")
    elif new_current_lines > 300:
        label_feedback.config(text="wonderful!", fg="gold")
    elif new_current_lines > 200:
        label_feedback.config(text="amazing!", fg="silver")
    elif new_current_lines > 100:
        label_feedback.config(text="very good!", fg="cyan")
    elif new_current_lines > 50:
        label_feedback.config(text="great!", fg="black")
    else:
        label_feedback.config(text="Keep going!", fg="black")

def on_submit(event=None):
    """提交按钮的回调函数,更新当前行数并保存"""
    global total_lines
    try:
        new_current_lines = int(entry_current_lines.get())
        total_lines += new_current_lines  # 直接加上当前行数
        write_record(total_lines)
        label_total_lines.config(text=f"累计总行数: {total_lines}")
        update_progress_bar()  # 更新进度条
        show_feedback(new_current_lines)  # 显示反馈信息

        tier_index, rank = get_tier_and_rank(total_lines)
        label_tier.config(text=f"当前层次: {tier_names[tier_index]} {rank}阶", fg=tier_colors[tier_index])

        messagebox.showinfo("保存成功", "当前行数已更新并保存。")
    except ValueError:
        messagebox.showerror("输入错误", "请输入有效的整数行数。")

# 读取之前的记录
total_lines = read_record()

# 创建图形化界面
root = tk.Tk()
root.title("代码行数记录器")

# 设置窗口宽度和位置
window_width = 400  # 宽度设为400像素
window_height = 300  # 高度设为300像素

# 获取屏幕宽度和高度
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()

# 计算窗口位置,使其位于右上角
x_position = screen_width - window_width
y_position = 0

# 设置窗口的大小和位置
root.geometry(f"{window_width}x{window_height}+{x_position}+{y_position}")

# 显示目标行数
label_target = tk.Label(root, text=f"目标行数: {TARGET_LINES}")
label_target.pack()

# 显示累计总行数
label_total_lines = tk.Label(root, text=f"累计总行数: {total_lines}")
label_total_lines.pack()

# 当前行数输入框
label_current = tk.Label(root, text="输入当前增加的行数:")
label_current.pack()

entry_current_lines = tk.Entry(root)
entry_current_lines.pack()

# 绑定回车键到提交函数
entry_current_lines.bind("<Return>", on_submit)

# 提交按钮
btn_submit = tk.Button(root, text="提交", command=on_submit)
btn_submit.pack()

# 进度条
progress_var = tk.DoubleVar()
progress_bar = ttk.Progressbar(root, variable=progress_var, maximum=100)
progress_bar.pack(fill=tk.X, padx=10, pady=10)

# 百分比显示标签
label_percentage = tk.Label(root, text="0.00%")
label_percentage.pack()

# 反馈信息标签
label_feedback = tk.Label(root, text="", font=("Arial", 14))
label_feedback.pack()

# 当前层次和等级标签
tier_index, rank = get_tier_and_rank(total_lines)
label_tier = tk.Label(root, text=f"当前层次: {tier_names[tier_index]} {rank}阶", fg=tier_colors[tier_index], font=("Arial", 14))
label_tier.pack()

# 初始化进度条
update_progress_bar()

# 运行图形化界面
root.mainloop()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值