Tkinter Python3 中,我明明给他定义了str,可他还说Nonetype,这是怎么回事?
以下是代码:——————————————————————————————————————importtkinterastkimporttkinterasTkimports...
以下是代码:
——————————————————————————————————————
import tkinter as tk
import tkinter as Tk
import sys
from tkinter import *
from tkinter import ttk
root=tk.Tk()
root.title("翻译器 1.0Beta 汉化版")
root.geometry('300x300')
root.resizable(0,0)
chinese_ORPMLZ={"你好":"eic","再见":"cie","对不起":"ice","没关系":"eci","我":"M","你":"A","她":"A_","它":"A-_","他":"A-","蟒蛇":"PN","命令":"CO","代码":"VW","文件":"F","视图":"WV","复习":"RW","退出":"Qz","中文":"CN","英语":"CE","数学":"MH","爪哇":"JV","我的世界":"MCT","系统":"SM","写":"WVW"}
def chinesemode():
chinesewin=tk.Tk()
chinesewin.title("中文模式")
chinesewin.geometry('500x300')
chinesewin.resizable(0,0)
chineseent=tk.Entry(chinesewin).pack(side='right').get()
chineselab=tk.Label(chinesewin,text=" 请输入中文:").pack(side='left')
def 翻译():
翻译win=tk.Tk()
翻译win.title("翻译内容")
翻译win.geometry('300x300')
翻译win.resizable(0,0)
try:
翻译lab=tk.Label(翻译win,text=chinese_ORPMLZ[str(chineseent)])
except KeyError:
Error=tk.Tk()
Error.title("Error1")
Error.geometry('300x300')
Error.resizable(0,0)
Errorbut1=tk.Button(Error,text='close',command=lambda:Error.distroy()).pack()
chinesebut=ttk.Button(chinesewin,text="翻译",command=翻译).pack(side='bottom')
chinesewin.mainloop()
chinesech=tk.Button(root,text="中文",command=chinesemode).pack()
root.mainloop()
print(type(chineseent.get()))
————————————————————————————————————
以下是输出(报错):
————————————————————————————————————
Exception in Tkinter callback
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter/__init__.py", line 1705, in __call__
return self.func(*args)
File "/Volumes/我才不是硬盘(D) /Python/点子/Tkinter/fanyiqi.py", line 17, in chinesemode
chineseent=tk.Entry(chinesewin).pack(side='right').get()
AttributeError: 'NoneType' object has no attribute 'get' 展开
——————————————————————————————————————
import tkinter as tk
import tkinter as Tk
import sys
from tkinter import *
from tkinter import ttk
root=tk.Tk()
root.title("翻译器 1.0Beta 汉化版")
root.geometry('300x300')
root.resizable(0,0)
chinese_ORPMLZ={"你好":"eic","再见":"cie","对不起":"ice","没关系":"eci","我":"M","你":"A","她":"A_","它":"A-_","他":"A-","蟒蛇":"PN","命令":"CO","代码":"VW","文件":"F","视图":"WV","复习":"RW","退出":"Qz","中文":"CN","英语":"CE","数学":"MH","爪哇":"JV","我的世界":"MCT","系统":"SM","写":"WVW"}
def chinesemode():
chinesewin=tk.Tk()
chinesewin.title("中文模式")
chinesewin.geometry('500x300')
chinesewin.resizable(0,0)
chineseent=tk.Entry(chinesewin).pack(side='right').get()
chineselab=tk.Label(chinesewin,text=" 请输入中文:").pack(side='left')
def 翻译():
翻译win=tk.Tk()
翻译win.title("翻译内容")
翻译win.geometry('300x300')
翻译win.resizable(0,0)
try:
翻译lab=tk.Label(翻译win,text=chinese_ORPMLZ[str(chineseent)])
except KeyError:
Error=tk.Tk()
Error.title("Error1")
Error.geometry('300x300')
Error.resizable(0,0)
Errorbut1=tk.Button(Error,text='close',command=lambda:Error.distroy()).pack()
chinesebut=ttk.Button(chinesewin,text="翻译",command=翻译).pack(side='bottom')
chinesewin.mainloop()
chinesech=tk.Button(root,text="中文",command=chinesemode).pack()
root.mainloop()
print(type(chineseent.get()))
————————————————————————————————————
以下是输出(报错):
————————————————————————————————————
Exception in Tkinter callback
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter/__init__.py", line 1705, in __call__
return self.func(*args)
File "/Volumes/我才不是硬盘(D) /Python/点子/Tkinter/fanyiqi.py", line 17, in chinesemode
chineseent=tk.Entry(chinesewin).pack(side='right').get()
AttributeError: 'NoneType' object has no attribute 'get' 展开
2个回答
展开全部
你好,那个Nonetyp是说你输入框当前的信息是Nonetyp。你原来的代码是在你创建那个输入框的时候直接读入输入的信息,所以你tk.Entry(chinesewin).pack(side='right').get()就会直接得到一个None的输入。下面是我帮你修改好的代码,可以跑的。你试试好了。
import tkinter as tk
import tkinter as Tk
import sys
from tkinter import *
from tkinter import ttk
from functools import partial
root=tk.Tk()
root.title("翻译器 1.0Beta 汉化版")
root.geometry('300x300')
root.resizable(0,0)
chinese_ORPMLZ={"你好":"eic","再见":"cie","对不起":"ice","没关系":"eci","我":"M","你":"A","她":"A_","它":"A-_","他":"A-","蟒蛇":"PN","命令":"CO","代码":"VW","文件":"F","视图":"WV","复习":"RW","退出":"Qz","中文":"CN","英语":"CE","数学":"MH","爪哇":"JV","我的世界":"MCT","系统":"SM","写":"WVW"}
def chinesemode():
chinesewin=tk.Tk()
chinesewin.title("中文模式")
chinesewin.geometry('500x300')
chinesewin.resizable(0,0)
chineselab=tk.Label(chinesewin,text=" 请输入中文:").pack(side='left')
chineseent=tk.Entry(chinesewin)
chinesebut=ttk.Button(chinesewin,text="翻译",command=partial(transfer, chineseent)).pack(side='bottom')
chineseent.pack(side='right')
chinesewin.mainloop()
#def on_button(self):
# print(self.entry.get())
def transfer(chineseent):
inputStr = chineseent.get()
if inputStr in chinese_ORPMLZ:
win=tk.Tk()
win.title("翻译结果")
win.geometry('300x300')
win.resizable(0,0)
print (chinese_ORPMLZ[str(inputStr)])
translab=tk.Label(win,text=chinese_ORPMLZ[str(inputStr)]).pack()
else:
Error=tk.Tk()
Error.title("Error1")
Error.geometry('300x300')
Error.resizable(0,0)
Errorbut1=tk.Button(Error,text='close',command=lambda:Error.distroy()).pack()
chinesech=tk.Button(root,text="中文",command=chinesemode).pack()
root.mainloop()
#print(type(chineseent.get()))
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询