我在输出结束时得到 { }。这是什么原因造成的,我该如何删除它?

Fan*_*tic 0 python tkinter

我正在尝试使用一个简单的 gui,它在同一个窗口中使用“秘密号码”/密钥标识符对消息进行加密和解密以获得乐趣。到目前为止,它有效,我对它非常满意。我唯一的问题是我在 crypt() 函数中得到了我想要的输出返回,然后是 {}。使用普通的 print() 函数,我没有这个问题。但是,使用 GUI 确实如此。我想这是包 tkinter 这样做吗?

我的代码:

from tkinter import *
import tkinter.messagebox


def crypt():
    so = int(nu1.get())
    inp = str(nu2.get().upper())
    old_dic = {chr(i): i * (int(so)+int(so))for i in range(ord("A"), ord("A") + 26)}   
    if len(inp) >= 1:
        bit = list(inp)
        spell = list(map(old_dic.get, bit))
        spell = spell[::-1]            
        ans1 = (*spell, "")
        blank4.insert(0, ans1)
        
    else:
        print("Improper input.")
        
main = Tk()
Label(main, text = "Enter secret number:").grid(row=0)
Label(main, text = "Cryptify:").grid(row=1)
Label(main, text = "Encrypted output:").grid(row=2)

Button(main, text='Show', command=crypt).grid(row=3, column=1, sticky=W, pady=4)

nu1 = Entry(main)
nu2 = Entry(main)
blank4 = Entry(main)
                   
nu1.grid(row=0, column=1)
nu2.grid(row=1, column=1)
blank4.grid(row=2, column=1)



mainloop()
Run Code Online (Sandbox Code Playgroud)

我得到的加密输出如下所示:

秘密号码:45

加密:你好

加密输出:7110 6840 6840 6210 6480 {}

输出应如下所示:

秘密号码:45

加密:你好

加密输出:7110 6840 6840 6210 6480

Coo*_*oud 5

原因很简单,tkinter用途tcl,它不知道什么蟒蛇list或者tuple是,它是tcl通过把它里面读的元组/列表方式{}就像你注意到没有,所以它转换为字符串,如:

ans1 = (*map(str,spell), "")
blank4.insert(0, ' '.join(ans1))
Run Code Online (Sandbox Code Playgroud)

对所有出现这种情况的地方也这样做。抱歉,我不久前发布了答案,互联网已关闭:/


我也不明白你为什么要在元组的末尾插入一个空字符串,你可以去掉它。

ans1 = map(str,spell)          
blank4.insert(0, ' '.join(ans1))
Run Code Online (Sandbox Code Playgroud)