Python:从另一个函数

时间:2016-12-12 14:10:55

标签: python function tkinter tkinter-entry

我正在尝试从另一个函数访问函数内的条目。但它告诉我它没有定义。我一直在研究很多,但我似乎无法找到任何可以帮助我的东西。

这是我的代码,我一直在缩短它以便让你们更轻松。虽然它应该足以让你有这个想法。

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)

1 个答案:

答案 0 :(得分:0)

您需要def BMHunter中的其他缩进来定义calculateFun

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

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

所以

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

您当前的代码将calculateFun作为同一类的另一个函数而不是BMHunter中的本地函数

我还建议将函数定义为def BMHunter(self),除非你故意想让它成为静态/类方法