在Tkinter Entry中转换某些文本的问题

时间:2017-06-07 19:08:14

标签: python tkinter

我编写了一个在控制台中运行良好的程序但后来我尝试将此程序与tkinter一起使用。该程序仍在工作,但转换后的文本不是在tkinter的Entry类中编写的文本。代码如下:

from tkinter import *

def dictionary():
    ascii_dictionary = {chr(i): bin(i)[2:] for i in range(128)}
    for i in ascii_dictionary:
        if len(ascii_dictionary[i]) < 8:
            count = 8 - len(ascii_dictionary[i])
            ascii_dictionary[i] = "".zfill(count) + ascii_dictionary[i]
    non_ascii_dictionary = {chr(i): bin(int(bytes(chr(i).encode(encoding="utf-8")).hex(), 16))[2:10] + " " +
                        bin(int(bytes(chr(i).encode(encoding="utf-8")).hex(), 16))[10:18] for i in range(128, 512)}
    dictionary = ascii_dictionary.copy()
    dictionary.update(non_ascii_dictionary)
    return dictionary

class Application():

    def __init__(self):
        self.window = Tk()
        self.frame_1 = Frame()
        self.frame_1.grid(row=0,column=0)
        self.widgets()
        self.mainloop = self.window.mainloop()

    def widgets(self):

        self.label_1 = Label(self.frame_1,text="Text To Binary:")
        self.label_1.grid(row=0,column=0)
        self.entry_1 = Entry(self.frame_1)
        self.entry_1.grid(row=0,column=1)
        self.button_1 = Button(self.frame_1,text="Convert",command=self.convert)
        self.button_1.grid(row=0,column=2)

    def convert(self):
        text_1=str(self.entry_1)
        return_value = dictionary()
        list_1 = [return_value[j] for i in text_1 for j in return_value if i == j]
        text_2 = " ".join(list_1)
        print(text_1,text_2)

instance=Application()

例如,当我输入&#34;你好&#34;到条目然后按转换按钮,我看到转换后的单词是&#34; .6138064.9675856&#34;。在您看来,问题是什么?

1 个答案:

答案 0 :(得分:2)

text_1=str(self.entry_1)

str(self.entry_1)获取entry_1小部件的字符串表示形式。窗口小部件的字符串表示形式是其唯一标识符,小数点和数字的集合。如果您想要条目中的文字,请使用get

text_1=str(self.entry_1.get())