<reponame>Georege/python_strings_handler<gh_stars>0
import base64

from tkinter import *
from tkinter import messagebox

from urllib.parse import urlencode, parse_qs, parse_qsl
import json


# 查看字符串长度
def getlen():
    source = source_input.get('0.0', 'end-1c')
    if source:
        output = len(source)
        # print('output type is :{}'.format(type(output)))
        target_output.config(state='normal')
        target_output.delete("1.0", 'end')
        target_output.insert('end', '长度为{}位'.format(output))
        target_output.config(state='disabled')
    else:
        messagebox.showinfo(
            '系统提示：', "输入框不能为空")


# 去重
def quchong():
    source = source_input.get('0.0', 'end-1c')
    if source:
        # print('source type is :{}'.format(type(source)))
        input_list = source.split('\n')
        # print('input_list type is :{}'.format(type(input_list)))
        output = '\n'.join([x for x in set(input_list) if x])
        # print('output type is :{}'.format(type(output)))
        target_output.config(state='normal')
        target_output.delete("1.0", 'end')
        target_output.insert('end', '{}'.format(output))
        target_output.config(state='disabled')
    else:
        messagebox.showinfo(
            '系统提示：', "输入框不能为空")


# 逗号拼接
def pinjie():
    source = source_input.get('0.0', 'end-1c')
    if source:
        # print('source type is :{}'.format(type(source)))
        input_list = source.split('\n')
        # print('input_list type is :{}'.format(type(input_list)))
        output = ','.join([x for x in input_list if x])
        # print('output type is :{}'.format(type(output)))
        target_output.config(state='normal')
        target_output.delete("1.0", 'end')
        target_output.insert('end', '{}'.format(output))
        target_output.config(state='disabled')
    else:
        messagebox.showinfo(
            '系统提示：', "输入框不能为空")


# &转换行
def convert_n():
    source = source_input.get('0.0', 'end-1c')
    if source:
        # print('source type is :{}'.format(type(source)))
        input_list = source.split('&')
        # print('input_list type is :{}'.format(type(input_list)))
        output = '\n'.join([x for x in input_list if x])
        # print('output type is :{}'.format(type(output)))
        target_output.config(state='normal')
        target_output.delete("1.0", 'end')
        target_output.insert('end', '{}'.format(output))
        target_output.config(state='disabled')
    else:
        messagebox.showinfo(
            '系统提示：', "输入框不能为空")


# @转换行
def convert_i():
    source = source_input.get('0.0', 'end-1c')
    if source:
        # print('source type is :{}'.format(type(source)))
        input_list = source.split('@')
        # print('input_list type is :{}'.format(type(input_list)))
        output = '\n'.join([x for x in input_list if x])
        # print('output type is :{}'.format(type(output)))
        target_output.config(state='normal')
        target_output.delete("1.0", 'end')
        target_output.insert('end', '{}'.format(output))
        target_output.config(state='disabled')
    else:
        messagebox.showinfo(
            '系统提示：', "输入框不能为空")


# 转义斜杠为短横
def convert():
    source = source_input.get('0.0', 'end-1c')
    if source:
        # print('source type is :{}'.format(type(source)))
        input_list = source.split('/')
        # print('input_list type is :{}'.format(type(input_list)))
        output = '-'.join([x for x in input_list if x])
        # print('output type is :{}'.format(type(output)))
        target_output.config(state='normal')
        target_output.delete("1.0", 'end')
        target_output.insert('end', '{}'.format(output))
        target_output.config(state='disabled')
    else:
        messagebox.showinfo(
            '系统提示：', "输入框不能为空")


# 转义字典为params,将存入的字典参数编码为URL查询字符串，即转换成以key1=value1&key2=value2的形式
def convert_params():
    source = source_input.get('0.0', 'end-1c')
    if source:
        # print('source type is :{}'.format(type(source)))
        output = urlencode(json.loads(source))
        # print('output type is :{}'.format(type(output)))
        target_output.config(state='normal')
        target_output.delete("1.0", 'end')
        target_output.insert('end', '{}'.format(output))
        target_output.config(state='disabled')
    else:
        messagebox.showinfo(
            '系统提示：', "输入框不能为空")


# 转义params为字典
def convert_dict():
    source = source_input.get('0.0', 'end-1c')
    if source:
        # print('source type is :{}'.format(type(source)))
        out_dict = parse_qs(source)
        output = json.dumps(
            {key: value[0] for key, value in out_dict.items()}, indent=4, ensure_ascii=False)
        # print('output type is :{}'.format(type(output)))
        target_output.config(state='normal')
        target_output.delete("1.0", 'end')
        target_output.insert('end', '{}'.format(output))
        target_output.config(state='disabled')
    else:
        messagebox.showinfo(
            '系统提示：', "输入框不能为空")


# 复制
def copy():
    output = target_output.get('0.0', 'end-1c')
    if output:
        root.clipboard_clear()
        root.clipboard_append(output)
        root.update()
        messagebox.showinfo('系统提示：', '{}\n复制成功'.format(output))
    else:
        messagebox.showinfo(
            '系统提示：', "输入框不能为空")


if __name__ == "__main__":
    input_out_width = 130
    input_out_height = 20
    
    root = Tk()
    root.title("字符串处理")
    root.resizable(width=False, height=False)

    # line 1
    Label(root, text="请输入需要处理的字符串：").grid(
        row=1, column=1)
    source_input = Text(root, width=input_out_width, height=input_out_height)
    source_input.grid(
        row=1, column=2, rowspan=2)
    # button
    Button(root, text="获取长度", command=lambda: getlen()).grid(
        row=1, column=3, padx=10)
    Button(root, text="列去重", command=lambda: quchong()).grid(
        row=1, column=4, padx=10)
    Button(root, text="逗号拼接", command=lambda: pinjie()).grid(
        row=1, column=5, padx=10)

    Button(root, text="转义斜杠", command=lambda: convert()).grid(
        row=2, column=3, padx=10)
    Button(root, text="转义&为换行", command=lambda: convert_n()).grid(
        row=2, column=4, padx=10)
    Button(root, text="转义@为换行", command=lambda: convert_i()).grid(
        row=2, column=5, padx=10)

    Button(root, text="转义字典为params", command=lambda: convert_params()).grid(
        row=3, column=3, padx=10)
    Button(root, text="转义params为字典", command=lambda: convert_dict()).grid(
        row=3, column=4, padx=10)
    # Button(root, text="转义@为换行", command=lambda: convert_i()).grid(
    #     row=3, column=5, padx=10)

    # line 2
    Label(root, text="处理后的结果是：").grid(
        row=3, column=1)
    target_output = Text(root, width=input_out_width, height=input_out_height)
    target_output.grid(
        row=3, column=2, rowspan=2)
    # button
    Button(root, text="复制", command=lambda: copy()).grid(
        row=4, column=3)

    root.mainloop()

import base64

from tkinter import *
from tkinter import messagebox

from urllib.parse import urlencode, parse_qs, parse_qsl
import json


# 查看字符串长度
def getlen():
    source = source_input.get('0.0', 'end-1c')
    if source:
        output = len(source)
        # print('output type is :{}'.format(type(output)))
        target_output.config(state='normal')
        target_output.delete("1.0", 'end')
        target_output.insert('end', '长度为{}位'.format(output))
        target_output.config(state='disabled')
    else:
        messagebox.showinfo(
            '系统提示：', "输入框不能为空")


# 去重
def quchong():
    source = source_input.get('0.0', 'end-1c')
    if source:
        # print('source type is :{}'.format(type(source)))
        input_list = source.split('\n')
        # print('input_list type is :{}'.format(type(input_list)))
        output = '\n'.join([x for x in set(input_list) if x])
        # print('output type is :{}'.format(type(output)))
        target_output.config(state='normal')
        target_output.delete("1.0", 'end')
        target_output.insert('end', '{}'.format(output))
        target_output.config(state='disabled')
    else:
        messagebox.showinfo(
            '系统提示：', "输入框不能为空")


# 逗号拼接
def pinjie():
    source = source_input.get('0.0', 'end-1c')
    if source:
        # print('source type is :{}'.format(type(source)))
        input_list = source.split('\n')
        # print('input_list type is :{}'.format(type(input_list)))
        output = ','.join([x for x in input_list if x])
        # print('output type is :{}'.format(type(output)))
        target_output.config(state='normal')
        target_output.delete("1.0", 'end')
        target_output.insert('end', '{}'.format(output))
        target_output.config(state='disabled')
    else:
        messagebox.showinfo(
            '系统提示：', "输入框不能为空")


# &转换行
def convert_n():
    source = source_input.get('0.0', 'end-1c')
    if source:
        # print('source type is :{}'.format(type(source)))
        input_list = source.split('&')
        # print('input_list type is :{}'.format(type(input_list)))
        output = '\n'.join([x for x in input_list if x])
        # print('output type is :{}'.format(type(output)))
        target_output.config(state='normal')
        target_output.delete("1.0", 'end')
        target_output.insert('end', '{}'.format(output))
        target_output.config(state='disabled')
    else:
        messagebox.showinfo(
            '系统提示：', "输入框不能为空")


# @转换行
def convert_i():
    source = source_input.get('0.0', 'end-1c')
    if source:
        # print('source type is :{}'.format(type(source)))
        input_list = source.split('@')
        # print('input_list type is :{}'.format(type(input_list)))
        output = '\n'.join([x for x in input_list if x])
        # print('output type is :{}'.format(type(output)))
        target_output.config(state='normal')
        target_output.delete("1.0", 'end')
        target_output.insert('end', '{}'.format(output))
        target_output.config(state='disabled')
    else:
        messagebox.showinfo(
            '系统提示：', "输入框不能为空")


# 转义斜杠为短横
def convert():
    source = source_input.get('0.0', 'end-1c')
    if source:
        # print('source type is :{}'.format(type(source)))
        input_list = source.split('/')
        # print('input_list type is :{}'.format(type(input_list)))
        output = '-'.join([x for x in input_list if x])
        # print('output type is :{}'.format(type(output)))
        target_output.config(state='normal')
        target_output.delete("1.0", 'end')
        target_output.insert('end', '{}'.format(output))
        target_output.config(state='disabled')
    else:
        messagebox.showinfo(
            '系统提示：', "输入框不能为空")


# 转义字典为params,将存入的字典参数编码为URL查询字符串，即转换成以key1=value1&key2=value2的形式
def convert_params():
    source = source_input.get('0.0', 'end-1c')
    if source:
        # print('source type is :{}'.format(type(source)))
        output = urlencode(json.loads(source))
        # print('output type is :{}'.format(type(output)))
        target_output.config(state='normal')
        target_output.delete("1.0", 'end')
        target_output.insert('end', '{}'.format(output))
        target_output.config(state='disabled')
    else:
        messagebox.showinfo(
            '系统提示：', "输入框不能为空")


# 转义params为字典
def convert_dict():
    source = source_input.get('0.0', 'end-1c')
    if source:
        # print('source type is :{}'.format(type(source)))
        out_dict = parse_qs(source)
        output = json.dumps(
            {key: value[0] for key, value in out_dict.items()}, indent=4, ensure_ascii=False)
        # print('output type is :{}'.format(type(output)))
        target_output.config(state='normal')
        target_output.delete("1.0", 'end')
        target_output.insert('end', '{}'.format(output))
        target_output.config(state='disabled')
    else:
        messagebox.showinfo(
            '系统提示：', "输入框不能为空")


# 复制
def copy():
    output = target_output.get('0.0', 'end-1c')
    if output:
        root.clipboard_clear()
        root.clipboard_append(output)
        root.update()
        messagebox.showinfo('系统提示：', '{}\n复制成功'.format(output))
    else:
        messagebox.showinfo(
            '系统提示：', "输入框不能为空")


if __name__ == "__main__":
    input_out_width = 130
    input_out_height = 20
    
    root = Tk()
    root.title("字符串处理")
    root.resizable(width=False, height=False)

    # line 1
    Label(root, text="请输入需要处理的字符串：").grid(
        row=1, column=1)
    source_input = Text(root, width=input_out_width, height=input_out_height)
    source_input.grid(
        row=1, column=2, rowspan=2)
    # button
    Button(root, text="获取长度", command=lambda: getlen()).grid(
        row=1, column=3, padx=10)
    Button(root, text="列去重", command=lambda: quchong()).grid(
        row=1, column=4, padx=10)
    Button(root, text="逗号拼接", command=lambda: pinjie()).grid(
        row=1, column=5, padx=10)

    Button(root, text="转义斜杠", command=lambda: convert()).grid(
        row=2, column=3, padx=10)
    Button(root, text="转义&为换行", command=lambda: convert_n()).grid(
        row=2, column=4, padx=10)
    Button(root, text="转义@为换行", command=lambda: convert_i()).grid(
        row=2, column=5, padx=10)

    Button(root, text="转义字典为params", command=lambda: convert_params()).grid(
        row=3, column=3, padx=10)
    Button(root, text="转义params为字典", command=lambda: convert_dict()).grid(
        row=3, column=4, padx=10)
    # Button(root, text="转义@为换行", command=lambda: convert_i()).grid(
    #     row=3, column=5, padx=10)

    # line 2
    Label(root, text="处理后的结果是：").grid(
        row=3, column=1)
    target_output = Text(root, width=input_out_width, height=input_out_height)
    target_output.grid(
        row=3, column=2, rowspan=2)
    # button
    Button(root, text="复制", command=lambda: copy()).grid(
        row=4, column=3)

    root.mainloop()
# 查看字符串长度 # print('output type is :{}'.format(type(output))) # 去重 # print('source type is :{}'.format(type(source))) # print('input_list type is :{}'.format(type(input_list))) # print('output type is :{}'.format(type(output))) # 逗号拼接 # print('source type is :{}'.format(type(source))) # print('input_list type is :{}'.format(type(input_list))) # print('output type is :{}'.format(type(output))) # &转换行 # print('source type is :{}'.format(type(source))) # print('input_list type is :{}'.format(type(input_list))) # print('output type is :{}'.format(type(output))) # @转换行 # print('source type is :{}'.format(type(source))) # print('input_list type is :{}'.format(type(input_list))) # print('output type is :{}'.format(type(output))) # 转义斜杠为短横 # print('source type is :{}'.format(type(source))) # print('input_list type is :{}'.format(type(input_list))) # print('output type is :{}'.format(type(output))) # 转义字典为params,将存入的字典参数编码为URL查询字符串，即转换成以key1=value1&key2=value2的形式 # print('source type is :{}'.format(type(source))) # print('output type is :{}'.format(type(output))) # 转义params为字典 # print('source type is :{}'.format(type(source))) # print('output type is :{}'.format(type(output))) # 复制 # line 1 # button # Button(root, text="转义@为换行", command=lambda: convert_i()).grid( #     row=3, column=5, padx=10) # line 2 # button