'NoneType'对象没有属性'get' | Python GUI

huangapple go评论56阅读模式
英文:

'NoneType' object has no attribute 'get' | Python GUI

问题

抱歉,我不能提供对代码错误的实时调试。这个错误通常是因为在尝试对一个没有返回任何值的对象执行 .get() 操作。你的代码似乎是将 Entry 组件的返回值直接赋值为变量(txt1 = Entry(root, width = 30).grid(...)),这实际上会将 txt1 赋值为 None,因为 grid() 方法没有返回一个可用于 .get() 操作的对象。

尝试分开创建和布局 Entry 组件,例如:

txt1 = Entry(root, width=30)
txt1.grid(row=0, column=1, pady=10, padx=10)

对于所有的 Entry 组件,都使用这种分开创建和布局的方法,而不是将其结合在一起。这样,txt1.get() 等操作将能够得到正确的 Entry 对象并返回其值。

至于Python版本,较旧的Python版本通常不会导致这种类型的错误。问题更可能是与代码逻辑和GUI组件创建相关。

英文:

So I'm writing a simple Python GUI program to generate random numbers and keep getting this error:
'NoneType' object has no attribute 'get'

def ButClick():
    try:
        MinNum = int (txt1.get())
        MaxNum = int (txt2.get())
        Num = int (txt3.get())
    except ValueError:
        messagebox.showerror("ValueError", "Error! Invalid numbers")
    else:
        Nums = ''
        if MinNum <= MaxNum:
            i = 0
            while i < Num:
                numOne = randint(MinNum,MaxNum)
                Nums = Nums + ":" + str(numOne)
                i += 1
            scr.insert(INSERT, str(Nums) + "\n")
        else:
            messagebox.showerror("NumError!!", "Error! Invalid Numbers!")
    pass

root = Tk()
root.title("Random is so random :)")

lb1 = Label(root, text = "Min number").grid(
    row = 0,
    column = 0,
    pady = 10,
    padx = 10)

txt1 = Entry(root, width = 30).grid(
    row = 0,
    column = 1,
    pady = 10,
    padx = 10)

lb2 = Label(root, text = "Max number").grid(
    row = 1,
    column = 0,
    pady = 10,
    padx = 10)

txt2 = Entry(root, width = 30).grid(
    row = 1,
    column = 1,
    pady = 10,
    padx = 10)

lb3 = Label(root, text = "number").grid(
    row = 2,
    column = 0,
    pady = 10,
    padx = 10)

txt3 = Entry(root, width = 30).grid(
    row = 2,
    column = 1,
    pady = 10,
    padx = 10)

but = Button(root, width = 15, height = 2, text = "Generate", command = ButClick).grid(
    row = 3,
    column = 0,
    columnspan = 2,
    pady = 10,
    padx = 10)

scr = scrolledtext.ScrolledText(root, height = 10).grid(
    row = 4,
    column = 0,
    columnspan = 2,
    pady = 10,
    padx = 10)

How can I fix it? Maybe its because I need to use the older version of Python?
I tried to install python 3.7.3 version via terminal but it was in vain.
I watched other questions on this topic but couldn't find any specifically on attribute .get

答案1

得分: 0

grid() 不返回任何内容,这就是为什么你的变量如txt2lb1都变成None,从而引发错误。我相信你想要做的是:

txt1 = Entry(root, width = 30)
txt1.grid(
    row = 0,
    column = 1,
    pady = 10,
    padx = 10)

当然,对于所有其他用户界面元素也是一样的。请注意代码首先创建一个元素并将其存储在一个变量中,然后单独将元素定位到网格中。

英文:

grid() does not return anything, which is why all your variables like txt2 and lb1 end up as None, hence the error. I believe what you want to do is:

txt1 = Entry(root, width = 30)
txt1.grid(
    row = 0,
    column = 1,
    pady = 10,
    padx = 10)

Of course same goes for all other UI elements. Notice how the code first creates an element and stores it in a variable, and only then, separately, positions the element in the grid.

huangapple
  • 本文由 发表于 2023年2月14日 19:12:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75446989.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定