字符串运算(Python)

 代码

# -*- coding: utf-8 -*-
# Environment    PyCharm
# File_name   frameString |User    Pfolg
# 2024/9/11   15:44
import tkinter as tk
from tkinter import ttk, messagebox, scrolledtext
import pyperclip




def reverseString(original_string="Hello, World!"):
    outst.config(state="normal")
    reversed_string = original_string[::-1]
    print(reversed_string)
    outst.delete("1.0", "end")
    outst.insert("end", reversed_string)
    outst.config(state="disabled")


def calculateString(original_string="Hello, World!"):
    outst.config(state="normal")
    length = len(original_string) - 1
    print(length)
    outst.delete("1.0", "end")
    outst.insert("end", str(length))
    outst.config(state="disabled")


def upperAndLower(original_string="Hello, World!"):
    outst.config(state="normal")
    s = ""
    for i in original_string:
        if i.isupper():
            s += i.lower()
        elif i.islower():
            s += i.upper()
        else:
            s += i

    print(s)
    outst.delete("1.0", "end")
    outst.insert("end", s)
    outst.config(state="disabled")


# 编码
def inCode(s: str):
    outst.config(state="normal")
    ns = s.encode()
    outst.delete("1.0", "end")
    outst.insert("end", str(ns))
    outst.config(state="disabled")


# 解码
def outCode(s: str):
    try:
        ns = eval(s)
        x = ns.decode()
    except BaseException:
        x = "数据错误"
    finally:
        outst.config(state="normal")
        outst.delete("1.0", "end")
        outst.insert("end", x)
        outst.config(state="disabled")


def inCode2(s: str):
    outst.config(state="normal")
    outst.delete("1.0", "end")
    for ns in s.encode():
        outst.insert("end", str(ns) + " ")
    outst.config(state="disabled")


def delete_out():
    outst.config(state="normal")
    outst.delete("1.0", "end")
    outst.config(state="disabled")


def copy_out():
    outst.config(state="normal")
    pyperclip.copy(outst.get("1.0", "end"))
    outst.config(state="disabled")


def paste():
    t = pyperclip.paste()
    st.delete("1.0", "end")
    st.insert("end", t)


def calculateStrs(e):
    outs = st.get("1.0", "end") * ent.get()
    outst.config(state="normal")
    outst.delete("1.0", "end")
    outst.insert("end", outs)
    outst.config(state="disabled")
    # ent.set(2)
    nt.place_forget()


def frameString(frame):
    ttk.Label(frame, text="字符串运算").place(relx=.45, rely=.02)
    ttk.Label(frame, text="IN").place(relx=.1, rely=.05)
    ttk.Label(frame, text="OUT").place(relx=.1, rely=.45)
    ttk.Label(frame, text="灵感来源:刘慈欣《宇宙坍缩》").place(relx=.02, rely=.95)

    global st, outst, nt, ent

    st = scrolledtext.ScrolledText(frame, width=80, height=10)
    st.place(relx=.1, rely=.1)
    outst = scrolledtext.ScrolledText(frame, width=60, height=10, state="disabled")
    outst.place(relx=.1, rely=.5)

    ent = tk.IntVar()
    nt = ttk.Entry(frame, textvariable=ent, width=3)
    ent.set(2)
    nt.bind("<Return>", calculateStrs)

    tk.Button(frame, text="Copy", command=lambda: pyperclip.copy(st.get("1.0", "end"))).place(relx=.9, rely=.1)
    tk.Button(frame, text="Clear", command=lambda: st.delete("1.0", "end")).place(relx=.9, rely=.2)
    tk.Button(frame, text="Paste", command=paste).place(relx=.9, rely=.3)
    tk.Button(frame, text="Copy", command=copy_out).place(relx=.5, rely=.85)
    tk.Button(frame, text="Clear", command=delete_out).place(relx=.6, rely=.85)


    Buttons = {
        "反转": lambda: reverseString(st.get("1.0", "end")),
        "长度": lambda: calculateString(st.get("1.0", "end")),
        "转码": lambda: upperAndLower(st.get("1.0", "end")),
        "解码": lambda: inCode(st.get("1.0", "end")),

        "间隔": "PGBox",  # 中间会跳过一个,拿这个来充数

        "转数": lambda: inCode2(st.get("1.0", "end")),
        "编码": lambda: outCode(st.get("1.0", "end")),
        "重复": lambda: nt.place(relx=.92, rely=.7),
        "压缩": lambda: messagebox.showinfo(title="提示信息", message="压缩请找AI")
    }
    j = .5  # y
    f = 0  # 计数
    left = .72  # 左列x
    for item in Buttons:
        if f <= 3:
            ttk.Button(
                frame, text=item, command=Buttons.get(item), width=8
            ).place(relx=left, rely=j)
            f += 1
            j += .1
        else:
            f = 0
            left += .1
            j = .5


if __name__ == '__main__':
    # pass
    window = tk.Tk()
    window.title(f"字符串运算")
    screen_w, screen_h = window.winfo_screenwidth(), window.winfo_screenheight()
    w, h = int(screen_w / 2), int(screen_h / 2)
    window.geometry(f'{w}x{h}+{int(screen_w / 4)}+{int(screen_h / 4)}')
    window.resizable(False, False)

    frameS = ttk.Frame(width=w, height=h)
    frameString(frameS)
    frameS.place(relx=0, rely=0)

    window.mainloop()

实现效果

反转

 转数

长度

 转码

大小写替换

 解码

 utf-8

 重复

可自定义重复数

 编码

utf-8

 压缩

 注

 该程序已集成至PGBox,将于2024/10发布,目前源码在GitHub上可用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值