Function Tkinter在类中调用函数

Function Tkinter在类中调用函数,function,class,python-3.x,tkinter,Function,Class,Python 3.x,Tkinter,在我的新程序中,我一直显示相同的代码行,所以我决定创建一个函数,并在需要时调用它。但我不断收到一个错误,告诉我“我的函数在未定义”。我是Python编程新手,我不知道该怎么做 这是我的代码: import tkinter as tk from tkinter import ttk class GUI(tk.Tk): def __init__(self): tk.Tk.__init__(self) #Window_Creation scr

在我的新程序中,我一直显示相同的代码行,所以我决定创建一个函数,并在需要时调用它。但我不断收到一个错误,告诉我“我的函数在未定义”。我是Python编程新手,我不知道该怎么做

这是我的代码:

import tkinter as tk
from tkinter import ttk

class GUI(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)

        #Window_Creation
        scr_xloc = int(self.winfo_screenwidth() / 2 - 800 / 2)
        scr_yloc = int(self.winfo_screenheight() / 2 - 600 / 2 - 30)

        self.geometry("800x600+{}+{}".format(scr_xloc, scr_yloc))
        self.minsize(width = 800, height = 600)
        ...

    def Factorial_Calculation():
        user_input = int(float(user_input))
        import math

        factorial_num = math.factorial(user_input)

        self.Output_Box.delete("1.0", "end")
        self.Output_Box.insert("1.0", str(user_input) + "! = " + str(factorial_num))

    def x_Factorial_Loop(self, event):
        global user_input
        ...

        Factorial_Calculation()

您的问题是调用
Factorial\u Calculation()
,您应该在类内调用它作为
self.Factorial\u Calculation()
,但在类外则是另一回事。在调用的函数前面添加“
self.
”,因为它应用于调用它的类,并从调用它的类中提取定义。

缩进可能已关闭。您是否混合使用制表符和空格?您得到的确切完整的错误消息是什么?NameError:名称“阶乘计算”未定义是否需要在括号内加上“self”?def Factorial_Calculation(self):精确,但仅适用于定义,否则在调用它时,只需执行通常的
Factorial_Calculation()