鱼C论坛

 找回密码
 立即注册
查看: 3151|回复: 2

[已解决]tkinter 报错

[复制链接]
发表于 2022-10-23 00:27:47 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
文件源码:
  1. # -*- coding:utf-8 -*-
  2. """/ *----------------------------------------------------------------
  3. 文件名:main.py
  4. 文件功能描述: 这是一个可以帮你生成计算题目的程序
  5. 创建者: asky533
  6. 时间:2022.10.6
  7. 编译软件: Pycharm Professional 2022.1
  8. ---------------------------------------------------------------- * /
  9. """
  10. from tkinter import *
  11. import random

  12. root = Tk()
  13. OPTIONS = ["addition", "subtraction", "multiplication", "division"]

  14. frame = Frame(root)
  15. frame.pack(padx=10, pady=10)

  16. v1 = StringVar()
  17. v2 = StringVar()
  18. v3 = StringVar()
  19. variable = StringVar()
  20. variable.set(OPTIONS[0])


  21. def function_one(content):
  22.     if content.isdigit():
  23.         return True
  24.     else:
  25.         return False


  26. testCMD = root.register(function_one)

  27. a = Label(frame, text="Please Enter The total of the calculation questions:").grid(row=0, column=0)

  28. b = Entry(frame, textvariable=v1, width=10, validate="key", validatecommand=(testCMD, "%P")).grid(row=0, column=1)

  29. Label(frame, text="Please Choose your mode: Addition, subtraction, multiplication or division:").grid(row=1, column=0)
  30. w = OptionMenu(root, variable, *OPTIONS)
  31. w.pack()


  32. def callback():
  33.     list = []
  34.     aides = 0
  35.     if w == OPTIONS[0]:
  36.         aides = "+"
  37.     elif w == OPTIONS[1]:
  38.         aides = "-"
  39.     elif w == OPTIONS[2]:
  40.         aides = "×"
  41.     elif w == OPTIONS[3]:
  42.         aides = "÷"

  43.     for x in range(a):
  44.         list.append(random.randrange(1, 100) + "." + random.randrange(1, 100) \
  45.                     + " " + aides + " " + random.randrange(1,100) + "." + random.randrange(1, 100)\
  46.                     + " ")

  47.     w1 = Message(root, text=list, width=100)
  48.     w1.pack()

  49. Button(root, text="Submit", command=callback).pack()

  50. mainloop()
复制代码


报错信息:
  1. Exception in Tkinter callback
  2. Traceback (most recent call last):
  3.   File "C:\Users\asky5\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 1921, in __call__
  4.     return self.func(*args)
  5.   File "C:\Users\asky5\pythonProject\main.py", line 56, in callback
  6.     for x in range(a):
  7. TypeError: 'NoneType' object cannot be interpreted as an integer
复制代码
最佳答案
2022-10-23 02:06:01
本帖最后由 hrpzcf 于 2022-10-23 02:16 编辑
  1. import random
  2. from tkinter import *

  3. root = Tk()
  4. OPTIONS = ["addition", "subtraction", "multiplication", "division"]

  5. frame = Frame(root)
  6. frame.pack(padx=10, pady=10)

  7. v1 = StringVar()
  8. v2 = StringVar()
  9. v3 = StringVar()
  10. variable = StringVar()
  11. variable.set(OPTIONS[0])


  12. def function_one(content):
  13.     if content.isdigit():
  14.         return True
  15.     else:
  16.         return False


  17. testCMD = root.register(function_one)

  18. a = Label(frame, text="Please Enter The total of the calculation questions:")
  19. a.grid(row=0, column=0)  # xxxxxxxxxxxx

  20. b = Entry(
  21.     frame, textvariable=v1, width=10, validate="key", validatecommand=(testCMD, "%P")
  22. )
  23. b.grid(row=0, column=1)  # xxxxxxxxxxxx

  24. Label(
  25.     frame,
  26.     text="Please Choose your mode: Addition, subtraction, multiplication or division:",
  27. ).grid(row=1, column=0)
  28. w = OptionMenu(root, variable, *OPTIONS)
  29. w.pack()


  30. def callback():
  31.     list = []
  32.     aides = ""  # xxxxxxxxxxxx
  33.     v = variable.get()
  34.     if v == OPTIONS[0]:  # xxxxxxxxxxxx
  35.         aides = "+"
  36.     elif v == OPTIONS[1]:  # xxxxxxxxxxxx
  37.         aides = "-"
  38.     elif v == OPTIONS[2]:  # xxxxxxxxxxxx
  39.         aides = "×"
  40.     elif v == OPTIONS[3]:  # xxxxxxxxxxxx
  41.         aides = "÷"

  42.     for x in range(int(b.get())):  # xxxxxxxxxxxx
  43.         list.append(
  44.             str(random.randrange(1, 100))  # xxxxxxxxxxxx
  45.             + "."
  46.             + str(random.randrange(1, 100))  # xxxxxxxxxxxx
  47.             + " "
  48.             + aides
  49.             + " "
  50.             + str(random.randrange(1, 100))  # xxxxxxxxxxxx
  51.             + "."
  52.             + str(random.randrange(1, 100))  # xxxxxxxxxxxx
  53.             + " "
  54.         )

  55.     w1 = Message(root, text=list, width=100)
  56.     w1.pack()


  57. Button(root, text="Submit", command=callback).pack()

  58. mainloop()
复制代码

评分

参与人数 1荣誉 +5 贡献 +3 收起 理由
高山 + 5 + 3 感谢楼主无私奉献!

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-10-23 01:26:46 | 显示全部楼层
你的a在callback自定义函数中都没有形参引入,也没有声明定义,当然就报错了。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-10-23 02:06:01 | 显示全部楼层    本楼为最佳答案   
本帖最后由 hrpzcf 于 2022-10-23 02:16 编辑
  1. import random
  2. from tkinter import *

  3. root = Tk()
  4. OPTIONS = ["addition", "subtraction", "multiplication", "division"]

  5. frame = Frame(root)
  6. frame.pack(padx=10, pady=10)

  7. v1 = StringVar()
  8. v2 = StringVar()
  9. v3 = StringVar()
  10. variable = StringVar()
  11. variable.set(OPTIONS[0])


  12. def function_one(content):
  13.     if content.isdigit():
  14.         return True
  15.     else:
  16.         return False


  17. testCMD = root.register(function_one)

  18. a = Label(frame, text="Please Enter The total of the calculation questions:")
  19. a.grid(row=0, column=0)  # xxxxxxxxxxxx

  20. b = Entry(
  21.     frame, textvariable=v1, width=10, validate="key", validatecommand=(testCMD, "%P")
  22. )
  23. b.grid(row=0, column=1)  # xxxxxxxxxxxx

  24. Label(
  25.     frame,
  26.     text="Please Choose your mode: Addition, subtraction, multiplication or division:",
  27. ).grid(row=1, column=0)
  28. w = OptionMenu(root, variable, *OPTIONS)
  29. w.pack()


  30. def callback():
  31.     list = []
  32.     aides = ""  # xxxxxxxxxxxx
  33.     v = variable.get()
  34.     if v == OPTIONS[0]:  # xxxxxxxxxxxx
  35.         aides = "+"
  36.     elif v == OPTIONS[1]:  # xxxxxxxxxxxx
  37.         aides = "-"
  38.     elif v == OPTIONS[2]:  # xxxxxxxxxxxx
  39.         aides = "×"
  40.     elif v == OPTIONS[3]:  # xxxxxxxxxxxx
  41.         aides = "÷"

  42.     for x in range(int(b.get())):  # xxxxxxxxxxxx
  43.         list.append(
  44.             str(random.randrange(1, 100))  # xxxxxxxxxxxx
  45.             + "."
  46.             + str(random.randrange(1, 100))  # xxxxxxxxxxxx
  47.             + " "
  48.             + aides
  49.             + " "
  50.             + str(random.randrange(1, 100))  # xxxxxxxxxxxx
  51.             + "."
  52.             + str(random.randrange(1, 100))  # xxxxxxxxxxxx
  53.             + " "
  54.         )

  55.     w1 = Message(root, text=list, width=100)
  56.     w1.pack()


  57. Button(root, text="Submit", command=callback).pack()

  58. mainloop()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-25 12:59

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表