我在输出结束时得到 { }.这是什么原因造成的,我该如何删除它? [英] I am getting { } on the end of my output. What is causing this and how do I remove it?

查看:21
本文介绍了我在输出结束时得到 { }.这是什么原因造成的,我该如何删除它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我的代码:

from tkinter import *导入 tkinter.messagebox定义加密():所以 = 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)}如果 len(inp) >= 1:位 = 列表(输入)拼写 = 列表(地图(old_dic.get,位))拼写=拼写[::-1]ans1 = (*拼写,")blank4.insert(0, ans1)别的:打印(不正确的输入.")主要 = Tk()标签(主要,文本=输入密码:").网格(行=0)标签(main, text = "Cryptify:").grid(row=1)标签(主要,文本=加密输出:").网格(行= 2)按钮(main, text='Show', command=crypt).grid(row=3, column=1,sticky=W, pady=4)nu1 = 条目(主要)nu2 = 条目(主要)空白 4 = 条目(主要)nu1.grid(行=0,列=1)nu2.grid(行=1,列=1)blank4.grid(row=2, column=1)主循环()

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

<块引用>

秘密编号:45

<块引用>

加密:你好

<块引用>

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

输出应如下所示:

<块引用>

秘密编号:45

<块引用>

加密:你好

<块引用>

加密输出:7110 6840 6840 6210 6480

解决方案

原因很简单,tkinter 使用 tcl 而它不知道 python listtuple 是,它是 tcl 读取元组/列表的方式,就像您注意到的那样,将其放入 {} 中,所以将其转换为字符串,例如:

ans1 = (*map(str,spell), "")blank4.insert(0, ' '.join(ans1))

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


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

ans1 = map(str,spell)blank4.insert(0, ' '.join(ans1))

I am trying my hand at a simple gui that encrypts and decrypts messages with a "secret number"/key identifier in the same window for fun. So far it works and I am pretty happy with it. My only issue is I am getting the output return in my crypt() function of what I want followed by {}. Using the normal print() function, I do not have that issue. However, using the GUI it does. I imagine it is the package tkinter doing this?

My code:

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()

Output for the Encryption I am getting looks like this:

Secret Number: 45

Cryptify: Hello

Encrypted Output: 7110 6840 6840 6210 6480 {}

Output should look like this:

Secret Number: 45

Cryptify: Hello

Encrypted Output: 7110 6840 6840 6210 6480

解决方案

The reason is quite simple, tkinter uses tcl and it does not know what python list or tuple are, and it is tcl way of reading tuples/lists by putting it inside {} like you noticed, so convert it to a string, like:

ans1 = (*map(str,spell), "")
blank4.insert(0, ' '.join(ans1))

Similarly do so for all the places where this is seen. Sorry, I had posted the answer a while back, internet got off :/


I also don't understand why you are inserting an empty string at the end of the tuple, you can get rid of it.

ans1 = map(str,spell)          
blank4.insert(0, ' '.join(ans1))

这篇关于我在输出结束时得到 { }.这是什么原因造成的,我该如何删除它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆