Tkinter使用自定义类时报AttributeError: 'Show' object has no attribute 'get'求助
错误原因
调用ent0.get()时,ent0是自定义Show类的实例,你没有在Show类中定义get方法,只有Tkinter原生的Entry组件实例才内置get()方法,因此抛出属性不存在的报错。当前使用全局变量存储ent的写法存在逻辑缺陷,多输入框场景下会出现值覆盖的问题。
修复方案
调整Show类定义
将Entry组件实例绑定为Show类的实例属性,同时封装对应的get方法:
from tkinter import Entry class Show: def __init__(self, WindowName=None, place_x=None, place_y=None): self.WindowName = WindowName self.place_y = place_y self.place_x = place_x self.entry_instance = None def create_entry(self): self.entry_instance = Entry(self.WindowName) self.entry_instance.place(x=self.place_x, y=self.place_y) def get(self): return self.entry_instance.get() if self.entry_instance else ""
调整调用逻辑
def AutoLoginPage(): ent0 = Show(WindowName=window, place_x=90, place_y=60) ent0.create_entry() # 正常调用get方法获取输入内容 user_input = ent0.get()
如果不需要额外封装,也可以直接访问实例的Entry属性调用原生方法:ent0.entry_instance.get(),同样可以拿到输入结果。
内容的提问来源于stack exchange,提问作者Amin
相关产品推荐
相关产品推荐

