所以我试图编写一些代码,它接受两个用户输入的值,比较它们,然后告诉用户他/她是否输入了正确的值。 (USL应该大于LSL,非常简单的标准)在这里你可以看到我如何从用户那里获得条目
# get USL and LSL
Label(self, text = "USL").grid(row = 5, column = 0, sticky = W, padx=5, pady=5)
self.e6 = Entry(self).grid(row = 5, column = 1)
Label(self, text = "LSL").grid(row = 6, column = 0, sticky = W, padx=5, pady=5)
self.e7 = Entry(self).grid(row = 6, column = 1)
下一段代码是我检查代码的地方
# button to check USL is higher than LSL
self.button = Button(self)
self.button["text"] = "Check USL and LSL"
self.button["command"] = self.check_limits
self.button.grid(row = 6, column = 2, sticky = W, padx=5, pady=5)
最后我在这里定义了check_limits函数
def check_limits(self):
e6 = int(self.e6)
e7 = int(self.e7)
if e6 > e7:
message = "Limits are good"
else:
message = "USL can't be less than LSL, please re-enter USL and LSL"
self.text.delete(0.0, END)
self.text.insert(0.0, message)
目前我将变量e6和e7强制为整数,但是当我这样做时,我得到一个错误,说int()参数必须是字符串或数字,而不是" NoneType"。为什么在这种情况下它们会被定义为null而不是整数?
或者,如果我不强迫他们成为整数,那么我总是得到" USL不能......",无论e6是大于还是小于比e7。为什么要这样做以及如何解决?
如果此问题的上下文需要其他任何代码,请查看我的整个代码
from Tkinter import *
class Application(Frame):
""" A SPC program that takes user input and saves the file """
def __init__(self,master):
""" initializes the frame """
Frame.__init__(self,master)
self.grid()
self.create_widgets()
def create_widgets(self):
"""create widgets for user inputted data"""
# get name
Label(self, text = "First Name").grid(row = 0, column = 0, sticky = W, padx=5, pady=5)
self.e1 = Entry(self).grid(row = 0, column = 1)
Label(self, text = "Last Name").grid(row = 1, column = 0, sticky = W, padx=5, pady=5)
self.e2 = Entry(self).grid(row = 1, column = 1)
# get work order
Label(self, text = "Work Order Number").grid(row = 2, column = 0, sticky = W, padx=5, pady=5)
self.e3 = Entry(self).grid(row = 2, column = 1)
# get todays date
Label(self, text = "Todays Date").grid(row = 3, column = 0, sticky = W, padx=5, pady=5)
self.e4 = Entry(self).grid(row = 3, column = 1)
# get bubble number
Label(self, text = "Bubble Number").grid(row = 4, column = 0, sticky = W, padx=5, pady=5)
self.e5 = Entry(self).grid(row = 4, column = 1)
# get USL and LSL
Label(self, text = "USL").grid(row = 5, column = 0, sticky = W, padx=5, pady=5)
self.e6 = Entry(self).grid(row = 5, column = 1)
Label(self, text = "LSL").grid(row = 6, column = 0, sticky = W, padx=5, pady=5)
self.e7 = Entry(self).grid(row = 6, column = 1)
# button to check USL is higher than LSL
self.button = Button(self)
self.button["text"] = "Check USL and LSL"
self.button["command"] = self.check_limits
self.button.grid(row = 6, column = 2, sticky = W, padx=5, pady=5)
# creates a spot to dictate whether USL and LSL are correct
self.text = Text(self, width = 35, height = 2, wrap = WORD)
self.text.grid(row = 6, column = 3, sticky = W)
def check_limits(self):
e6 = int(self.e6)
e7 = int(self.e7)
if e6 > e7:
message = "Limits are good"
else:
message = "USL can't be less than LSL, please re-enter USL and LSL"
self.text.delete(0.0, END)
self.text.insert(0.0, message)
root = Tk()
root.title("SPC Input Program")
root.geometry("700x500")
app = Application(root)
root.mainloop()
答案 0 :(得分:1)
你的问题是这个
self.e6 = Entry(self).grid(row = 5, column = 1)
如果要使用指定的窗口小部件,则无法打包,放置或网格化。你需要把它改成这个
self.e6 = Entry(self)
self.e6.grid(row = 5, column = 1)
由于网格分配,您获得NoneType
。对于您不打算访问的标签和按钮等静态窗口小部件,这样做很好。但是当您想要获取或为窗口小部件赋值时,它需要是窗口小部件而不是widget.grid()