html按钮属性保存文本框,Tkinter:使用按钮单击命令获取文本框值并保存在html中(示例代码)...

我创建了一个小程序来获取用户输入的文本并将文本保存在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()。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值