2016-03-03 83 views
3

在我的新程序中,我保持显示相同的代码行,所以我决定创建一个函数,并在需要时调用它。但我不断收到一个错误,告诉我“我的功能没有定义”。我是Python编程的新手,我无法想象它!Tkinter在类中调用函数

这是我的代码:

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() 
+0

您的缩进可能已关闭。你有混合的制表符和空格吗? – bernie

+0

你得到的确切完整的错误信息是什么? –

+0

NameError:名称'Factorial_Calculation'未定义 –

回答

2

你的问题是Factorial_Calculation()调用,你应该在类中称其为self.Factorial_Calculation(),但外部类是另一回事。将“self.”添加到被调用函数的前面,因为它适用于您调用该类的类,并从您调用它的类中提取定义。

+0

是否需要在括号内放置“self”? def Factorial_Calculation(self): –

+0

准确地说,但仅用于定义,否则当调用它时,只需执行通常的'Factorial_Calculation()', –