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

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

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

问题

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

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

  1. txt1 = Entry(root, width=30)
  2. 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'

  1. def ButClick():
  2. try:
  3. MinNum = int (txt1.get())
  4. MaxNum = int (txt2.get())
  5. Num = int (txt3.get())
  6. except ValueError:
  7. messagebox.showerror("ValueError", "Error! Invalid numbers")
  8. else:
  9. Nums = ''
  10. if MinNum <= MaxNum:
  11. i = 0
  12. while i < Num:
  13. numOne = randint(MinNum,MaxNum)
  14. Nums = Nums + ":" + str(numOne)
  15. i += 1
  16. scr.insert(INSERT, str(Nums) + "\n")
  17. else:
  18. messagebox.showerror("NumError!!", "Error! Invalid Numbers!")
  19. pass
  20. root = Tk()
  21. root.title("Random is so random :)")
  22. lb1 = Label(root, text = "Min number").grid(
  23. row = 0,
  24. column = 0,
  25. pady = 10,
  26. padx = 10)
  27. txt1 = Entry(root, width = 30).grid(
  28. row = 0,
  29. column = 1,
  30. pady = 10,
  31. padx = 10)
  32. lb2 = Label(root, text = "Max number").grid(
  33. row = 1,
  34. column = 0,
  35. pady = 10,
  36. padx = 10)
  37. txt2 = Entry(root, width = 30).grid(
  38. row = 1,
  39. column = 1,
  40. pady = 10,
  41. padx = 10)
  42. lb3 = Label(root, text = "number").grid(
  43. row = 2,
  44. column = 0,
  45. pady = 10,
  46. padx = 10)
  47. txt3 = Entry(root, width = 30).grid(
  48. row = 2,
  49. column = 1,
  50. pady = 10,
  51. padx = 10)
  52. but = Button(root, width = 15, height = 2, text = "Generate", command = ButClick).grid(
  53. row = 3,
  54. column = 0,
  55. columnspan = 2,
  56. pady = 10,
  57. padx = 10)
  58. scr = scrolledtext.ScrolledText(root, height = 10).grid(
  59. row = 4,
  60. column = 0,
  61. columnspan = 2,
  62. pady = 10,
  63. 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,从而引发错误。我相信你想要做的是:

  1. txt1 = Entry(root, width = 30)
  2. txt1.grid(
  3. row = 0,
  4. column = 1,
  5. pady = 10,
  6. 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:

  1. txt1 = Entry(root, width = 30)
  2. txt1.grid(
  3. row = 0,
  4. column = 1,
  5. pady = 10,
  6. 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:

确定