在我的新程序中,我不断出现相同的代码行,所以我决定创建一个函数,并在需要时调用它。但是我不断收到一个错误,告诉我“我的函数未定义”。我是 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_Calculation()
的调用,您应该在类内调用它作为 self.Factorial_Calculation()
但在类外是另一回事。在被调用函数前添加“self.
”,因为它适用于您调用它的类,并从您调用它的类中提取定义。
关于function - Tkinter 在类中调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35776551/