在Python tkinter按钮中使用.get的困难

时间:2014-08-21 13:59:56

标签: python user-interface tkinter

我一直在研究一个简单的GUI并尝试接收输入数据思想文本框。然后在按下按钮时,将计算数据(宽度,高度和长度)并显示在标签中。

这只是我m having issues with, no calculations involved yet, I尝试的程序的体积计算部分。输入数据进行打印并确保其正常工作,并且能够使其正常工作。我必须遗漏一些最小的东西。

import sys
from tkinter import *
from tkinter import ttk

windowGUI = Tk()
""" ---------------Volume Calculator---------------- """

labelWidth = Label(windowGUI, text='Width').grid(row=0,column=3,sticky=W)
labelHeight = Label(windowGUI, text='Height').grid(row=0,column=4,sticky=W)
labelLength = Label(windowGUI, text='Length').grid(row=0,column=5,sticky=W)

calcWidth = Entry(windowGUI).grid(row=1,column=3,sticky=W)
calcHeight = Entry(windowGUI).grid(row=1,column=4,sticky=W)
calcLength = Entry(windowGUI).grid(row=1,column=5,sticky=W)

def calculate():
   print (calcWidth.get(), calcHeight.get(), calcLength.get())

calcButt = Button(windowGUI, text='Calculate', command=calculate)
calcButt.grid(row=3,column=4,sticky=W,pady=4)

windowGUI.mainloop()

我收到的错误是"属性错误:' NoneType'对象没有属性' get'"

这是我第一次使用tkinter在python上的经验,非常感谢帮助/建议谢谢!

2 个答案:

答案 0 :(得分:1)

calcWidth = Entry(windowGUI).grid(row=1,column=3,sticky=W)

在此行中,您说“创建条目,然后在其上调用grid。将grid来电的结果分配给calcWidth。”这是一个问题,因为grid的结果始终为None。你应该在不同的行上进行赋值和网格化。

calcWidth = Entry(windowGUI)
calcWidth.grid(row=1,column=3,sticky=W)

答案 1 :(得分:0)

使用此

import sys
from Tkinter import *

windowGUI = Tk()
""" ---------------Volume Calculator---------------- """

labelWidth = Label(windowGUI, text='Width').grid(row=0,column=3,sticky=W)
labelHeight = Label(windowGUI, text='Height').grid(row=0,column=4,sticky=W)
labelLength = Label(windowGUI, text='Length').grid(row=0,column=5,sticky=W)

calcWidth = Entry(windowGUI)
calcWidth.grid(row=1,column=3,sticky=W)
calcHeight = Entry(windowGUI)
calcHeight.grid(row=1,column=4,sticky=W)
calcLength = Entry(windowGUI)
calcLength.grid(row=1,column=5,sticky=W)

def calculate():
    print (calcWidth.get(), calcHeight.get(), calcLength.get())

calcButt = Button(windowGUI, text='Calculate', command=calculate)
calcButt.grid(row=3,column=4,sticky=W,pady=4)

windowGUI.mainloop()

[注]: 你的代码中还写了“from tkinter import ttk”。删除此行,此行是不必要的,并生成错误。