猿问

在 tkinter 条目小部件中输入的数据包含“NoneType”对象并且没有属性“get”

我正在尝试创建一个收集实验数据的程序。我已成功创建了一个包含标签和条目小部件的表。但是,当尝试通过收集输入的数据使条目小部件起作用时,我的脚本失败了。


import tkinter as tk

from PIL import ImageTk, Image



class Window(tk.Frame):


def __init__(self, master=None):

   tk.Frame.__init__(self, master)               

   self.master = master

   self.init_window()

def init_window(self):

   self.master.title("Data Analyser")


   tk.Label(self.master, text="Cell Number").grid(row=1, sticky=tk.W)

   tk.Label(self.master, text="1").grid(row=2) 

   tk.Label(self.master, text="1").grid(row=2)      



   tk.Label(self.master, text="Treatment").grid(row=1, column=2)

   Treatment_1 = tk.Entry(self.master).grid(row=2,column=2)

   Treatment_2 = tk.Entry(self.master).grid(row=3,column=2)


   tk.Button(self.master, text = "Submit", command = self.getInput).grid(row = 14, column = 2)


def getInput(self):    

   global Treatment_Data

   a = Treatment_1.get()

   b = Treatment_2.get()

   Treatment_Data = [a,b]




root = tk.Tk()

root.geometry("500x450")

app = Window(root) 

root.mainloop()                                                                                                                                                               

我希望上面的代码会生成一个名为“Treatment_Data”的全局变量,但不幸的是,当我单击“提交”小部件时收到以下错误消息:


 Traceback (most recent call last):

    File "C:\Users\xxxx\lib\tkinter\__init__.py", line 1550, in __call__

    return self.func(*args)

    File "<ipython-input-134-2b5d212a0dcf>", line 47, in getInput

       a = self.Treatment_1.get()

  AttributeError: 'NoneType' object has no attribute 'get'

任何澄清将不胜感激。


慕标5832272
浏览 191回答 1
1回答

智慧大石

您需要使用 self 将 Treatment_1 和 Treatment_2 作为类变量,然后才能在回调函数 getInput() 中访问它们。评论中提到的另一件事是您需要将两部分分开 -1. 创建一个 Entry Widget 和2. 将其放置在网格中。您应该创建一个 Entry 小部件并将其存储在 Treatment_1 中。完成后,您应该使用 Treatment_1 变量将小部件放置在网格中的所需位置。import tkinter as tkfrom PIL import ImageTk, Imageclass Window(tk.Frame):&nbsp; &nbsp;def __init__(self, master=None):&nbsp; &nbsp; &nbsp; tk.Frame.__init__(self, master)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; self.master = master&nbsp; &nbsp; &nbsp; self.init_window()&nbsp; &nbsp;def init_window(self):&nbsp; &nbsp; &nbsp; self.master.title("Data Analyser")&nbsp; &nbsp; &nbsp; tk.Label(self.master, text="Cell Number").grid(row=1, sticky=tk.W)&nbsp; &nbsp; &nbsp; tk.Label(self.master, text="1").grid(row=2)&nbsp;&nbsp; &nbsp; &nbsp; tk.Label(self.master, text="1").grid(row=2)&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; tk.Label(self.master, text="Treatment").grid(row=1, column=2)&nbsp; &nbsp; &nbsp; self.Treatment_1 = tk.Entry(self.master)&nbsp; &nbsp; &nbsp; self.Treatment_2 = tk.Entry(self.master)&nbsp; &nbsp; &nbsp; self.Treatment_1.grid(row=2,column=2)&nbsp; &nbsp; &nbsp; self.Treatment_2.grid(row=3,column=2)&nbsp; &nbsp; &nbsp; tk.Button(self.master, text = "Submit", command = self.getInput).grid(row = 14, column = 2)&nbsp; &nbsp;def getInput(self):&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; global Treatment_Data&nbsp; &nbsp; &nbsp; a = self.Treatment_1.get()&nbsp; &nbsp; &nbsp; b = self.Treatment_2.get()&nbsp; &nbsp; &nbsp; Treatment_Data = [a,b]&nbsp; &nbsp; &nbsp; print(Treatment_Data)root = tk.Tk()root.geometry("500x450")app = Window(root)&nbsp;root.mainloop()
随时随地看视频慕课网APP

相关分类

Python
我要回答