Python 转换Tkinter条目中的某些文本时出现问题
我编写了一个在控制台中运行良好的程序,但后来我尝试将此程序与tkinter一起使用。程序仍在运行,但转换后的文本不是在tkinter的Entry类中编写的文本。代码如下:Python 转换Tkinter条目中的某些文本时出现问题,python,tkinter,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_diction
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()
从tkinter导入*
def dictionary():
ascii_字典={chr(i):bin(i)[2:]表示范围(128)内的i}
对于ascii_字典中的i:
如果len(ascii_字典[i])<8:
count=8-len(ascii\U字典[i])
ascii_字典[i]=“.zfill(count)+ascii_字典[i]
非ascii字典={chr(i):bin(int(bytes(chr(i).encode(encoding=“utf-8”)).hex(),16))[2:10]+“”+
bin(int(字节(chr(i).encode(encoding=“utf-8”)).hex(),16))[10:18]表示范围(128512)内的i)
dictionary=ascii_dictionary.copy()
字典更新(非ascii字典)
返回字典
类应用程序():
定义初始化(自):
self.window=Tk()
self.frame_1=frame()
self.frame_1.grid(行=0,列=0)
self.widgets()
self.mainloop=self.window.mainloop()
def小部件(自):
self.label\u 1=标签(self.frame\u 1,text=“文本到二进制:”)
self.label_1.grid(行=0,列=0)
self.entry_1=条目(self.frame_1)
self.entry_1.grid(行=0,列=1)
self.button\u 1=按钮(self.frame\u 1,text=“Convert”,command=self.Convert)
self.button_1.grid(行=0,列=2)
def转换(自):
text_1=str(self.entry_1)
返回值=字典()
列表_1=[文本中i的返回_值[j];如果i==j,则返回_值中j的返回_值]
text_2=”“.加入(列表_1)
打印(文本1、文本2)
实例=应用程序()
例如,当我对条目键入“hello”,然后按convert按钮时,我看到转换后的单词是“.6138064.9675856”。在你看来,问题是什么
text_1=str(self.entry_1)
str(self.entry\u 1)
获取entry\u 1
小部件的字符串表示形式。小部件的字符串表示是其唯一标识符,即小数点和数字的集合。如果要在条目中输入文本,请使用get
text_1=str(self.entry_1.get())
str(self.entry\u 1)
获取entry\u 1
小部件的字符串表示形式。小部件的字符串表示是其唯一标识符,即小数点和数字的集合。如果要在条目中输入文本,请使用get
text_1=str(self.entry_1.get())