Python:从另一个函数访问内部函数

2024-05-23 18:19:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从另一个函数访问函数内部的一个条目。但它告诉我它没有被定义。我做了很多研究,但似乎找不到任何对我有帮助的东西。在

这是我的代码,我一直在缩短它,让你们更容易。虽然这应该足够让你知道。在

from tkinter import*

class hunterClass:

    def BMHunter():
        hunter = Tk()
        hunter.title("Beast Mastery Gear Worth Calculator")

        agiString = StringVar()

        agiText = Label(hunter, text = "Input points of agility:").grid(row = 3, column = 0)
        agiEntry = Entry(hunter, textvariable = agiString).grid(row = 3, column = 1)

        calculate = Button(hunter, text = "Calculate", command = hunterClass.calculateFun).grid(row = 12, column = 1)

    def calculateFun():
        agiF = float(agiEntry.get())

window = Tk()
window.title("Gear Worth Calculator")

hunterText = Label(window, text = "Hunter").grid(row = 3, column = 0)
hunterBM = Button(window, text = "Beast Mastery", width = 12, command = hunterClass.BMHunter).grid(row = 4, column = 0)

Tags: 函数texttitledefcolumnwindowtkgrid
1条回答
网友
1楼 · 发布于 2024-05-23 18:19:04

您需要在def BMHunter中另一个缩进来定义calculateFun

我假设您正在尝试在python中使用闭包:

https://www.programiz.com/python-programming/closure

所以

def BMHunter():
    ....
    def calculateFun()

您当前的代码将calculateFun作为同一类的另一个函数,而不是BMHunter内的局部函数

我还建议将函数定义为defBMHunter(self),除非您有意将其设为静态/类方法

相关问题 更多 >