我创建了一个小程序来获取用户输入的文本并将文本保存在html文件中。这是我的示例小程序。
from tkinter import *
from jinja2 import Template
root=Tk()
textBox=Text(root, height=2, width=10)
textBox.pack()
buttonSave=Button(root, height=1, width=10, text="Save",
command=lambda: createCaseDetails())
buttonSave.pack()
def createCaseDetails():
# Create The template html for case report
t = Template("""
Case Report<Evidence> System Information
Created with love by bluebear119
Case Description
{{Case_Description}
""")
f = open('Case Report.html', 'w')
inputvalue = textBox.get("1.0", "end-1c")
print(inputvalue)
message = t.render(Case_Description=inputvalue)
f.write(message)
f.close()
print("Case Report.html file saved.")
mainloop()
但是当我在我的巨大代码中实现它时,我不能使用变量,因为它来自同一个类但是在另一个函数的变量中。我在顶层定义了createCaseDetails()函数,但我的文本框在另一个函数中,按钮也在另一个函数中。如何按下按钮并将案例描述文本保存在html中。
文本框和按钮将在同一个类中定义,如下所示:
Class CreateCaseInterface(Frame):
def caseInformation(self):
...
Case_Description_text=Text(self.caseInfoFrame,width=30,height=11,yscrollcommand=CaseDescripyscrollbar.set)
Case_Description_text.grid(row =4, column =1,pady=5)
def UACaseManagement(self):
Executebtn = Button(self.UACaseManagementFrame, text="Execute for create and save the case file", command=createCaseDetails(self),width=30,height=5)
Executebtn.grid(row=12,column= 4,sticky=W,pady=5,columnspan=2)
def createCaseDetails(self):
...
# As I already know declare global is not the solution
inputvalue = Case_Description_text.get("1.0", "end-1c")
print(inputvalue)
message = t.render(Case_Description_text=inputvalue)
该错误将无法在Case_Description_text函数中使用变量createCaseDetails()。