鱼C论坛

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

[已解决]tkinter 报错

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

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

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

x
文件源码:
# -*- 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[0])


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[0]:
        aides = "+"
    elif w == OPTIONS[1]:
        aides = "-"
    elif w == OPTIONS[2]:
        aides = "×"
    elif w == OPTIONS[3]:
        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
最佳答案
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[0])


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[0]:  # xxxxxxxxxxxx
        aides = "+"
    elif v == OPTIONS[1]:  # xxxxxxxxxxxx
        aides = "-"
    elif v == OPTIONS[2]:  # xxxxxxxxxxxx
        aides = "×"
    elif v == OPTIONS[3]:  # 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荣誉 +5 贡献 +3 收起 理由
高山 + 5 + 3 感谢楼主无私奉献!

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

使用道具 举报

发表于 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[0])


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[0]:  # xxxxxxxxxxxx
        aides = "+"
    elif v == OPTIONS[1]:  # xxxxxxxxxxxx
        aides = "-"
    elif v == OPTIONS[2]:  # xxxxxxxxxxxx
        aides = "×"
    elif v == OPTIONS[3]:  # 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()
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-9-25 23:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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