asky533 发表于 2022-10-23 00:27:47

tkinter 报错

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

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

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

v1 = StringVar()
v2 = StringVar()
v3 = StringVar()
variable = StringVar()
variable.set(OPTIONS)


def function_one(content):
    if content.isdigit():
      return True
    else:
      return False


testCMD = root.register(function_one)

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

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

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


def callback():
    list = []
    aides = 0
    if w == OPTIONS:
      aides = "+"
    elif w == OPTIONS:
      aides = "-"
    elif w == OPTIONS:
      aides = "×"
    elif w == OPTIONS:
      aides = "÷"

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

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

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

mainloop()

报错信息:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\asky5\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 1921, in __call__
    return self.func(*args)
File "C:\Users\asky5\pythonProject\main.py", line 56, in callback
    for x in range(a):
TypeError: 'NoneType' object cannot be interpreted as an integer

lassiter 发表于 2022-10-23 01:26:46

你的a在callback自定义函数中都没有形参引入,也没有声明定义,当然就报错了。

hrpzcf 发表于 2022-10-23 02:06:01

本帖最后由 hrpzcf 于 2022-10-23 02:16 编辑

import random
from tkinter import *

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

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

v1 = StringVar()
v2 = StringVar()
v3 = StringVar()
variable = StringVar()
variable.set(OPTIONS)


def function_one(content):
    if content.isdigit():
      return True
    else:
      return False


testCMD = root.register(function_one)

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

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

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


def callback():
    list = []
    aides = ""# xxxxxxxxxxxx
    v = variable.get()
    if v == OPTIONS:# xxxxxxxxxxxx
      aides = "+"
    elif v == OPTIONS:# xxxxxxxxxxxx
      aides = "-"
    elif v == OPTIONS:# xxxxxxxxxxxx
      aides = "×"
    elif v == OPTIONS:# xxxxxxxxxxxx
      aides = "÷"

    for x in range(int(b.get())):# xxxxxxxxxxxx
      list.append(
            str(random.randrange(1, 100))# xxxxxxxxxxxx
            + "."
            + str(random.randrange(1, 100))# xxxxxxxxxxxx
            + " "
            + aides
            + " "
            + str(random.randrange(1, 100))# xxxxxxxxxxxx
            + "."
            + str(random.randrange(1, 100))# xxxxxxxxxxxx
            + " "
      )

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


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

mainloop()
页: [1]
查看完整版本: tkinter 报错