我有下面的代码,使用tkinter创建一个python GUI,它向用户提供了一些选项,如两个复选框和两个填充文本填充。我试图让程序创建一个文件,当用户单击go按钮时,该文件填充模板中的字段。例如,如果客人名称是charles marriott。我正在尝试创建一个名为charles marriott.html的文件,其中包含一个类似"欢迎%s的语句到酒店。我们看到你将搭乘飞机前往酒店。"我是否必须创建另一个功能来执行此操作,这甚至可以与Tkinter一起使用,还是我需要使用另一个我不知道的库?
from Tkinter import *
class App:
def __init__(self, master):
column0_padx = 24
row_pady = 36
#Label 1
lbl_testcase_exec = Label(master, text="Visiting by Train",
wraplength=100, anchor='w', justify='left')
lbl_results_cmp = Label(master, text="Visting by Plane",
wraplength=100, justify='left')
testcase_exec = Checkbutton(master)
results_cmp = Checkbutton(master)
tolerance = Entry(master, width=4)
lbl_analysis = Label(master, text="Guest Name")
analysis_lib = Entry(master, width=30)
lbl_testcase_exec.grid(row=0, column=2, padx=20, pady=12, sticky='w')
lbl_results_cmp.grid(row=0, column=3, pady=12, sticky='w')
lbl_analysis.grid(row=1, column=0, sticky='w', padx=column0_padx)
analysis_lib.grid(row=1, column=1, sticky='w')
testcase_exec.grid(row=1, column=2, padx=20, sticky='w')
results_cmp.grid(row=1, column=3, sticky='w')
tolerance.grid(row=1, column=4, padx=20, sticky='w')
#Label 2
lbl_ref_analysis = Label(
master, text="Point of Contact",
wraplength=150, justify='left', pady=row_pady)
ref_analysis_lib = Entry(master, width=30)
lbl_ref_analysis.grid(row=2, column=0, sticky='w', padx=column0_padx)
ref_analysis_lib.grid(row=2, column=1, sticky='w')
# buttons
bottom_frame = Frame(master)
bottom_frame.grid(row=5, column=1, columnspan=3, sticky='w')
btn_start = Button(bottom_frame, text = "Go", width=7)
btn_start.pack(side='left')
btn_commit = Button(bottom_frame, text="Commit", width=7)
btn_commit.pack(side='left', padx=80)
btn_exit = Button(bottom_frame, text="Exit", width=7)
btn_exit.pack(side='left')
root = Tk()
root.title("Templates")
root.minsize(800, 400)
app = App(root)
root.mainloop()
答案 0 :(得分:1)
向创建文件的class App
添加另一种方法,然后通过使用command=self.newmethod
配置按钮将其与按钮关联相关联。
以下是基于您的代码的最小示例:
from Tkinter import *
class App:
def __init__(self, master):
column0_padx = 24
row_pady = 36
#Label 1
lbl_testcase_exec = Label(master, text="Visiting by Train",
wraplength=100, anchor='w', justify='left')
lbl_results_cmp = Label(master, text="Visting by Plane",
wraplength=100, justify='left')
testcase_exec = Checkbutton(master)
results_cmp = Checkbutton(master)
tolerance = Entry(master, width=4)
lbl_analysis = Label(master, text="Guest Name")
analysis_lib = Entry(master, width=30)
lbl_testcase_exec.grid(row=0, column=2, padx=20, pady=12, sticky='w')
lbl_results_cmp.grid(row=0, column=3, pady=12, sticky='w')
lbl_analysis.grid(row=1, column=0, sticky='w', padx=column0_padx)
analysis_lib.grid(row=1, column=1, sticky='w')
testcase_exec.grid(row=1, column=2, padx=20, sticky='w')
results_cmp.grid(row=1, column=3, sticky='w')
tolerance.grid(row=1, column=4, padx=20, sticky='w')
#Label 2
lbl_ref_analysis = Label(
master, text="Point of Contact",
wraplength=150, justify='left', pady=row_pady)
ref_analysis_lib = Entry(master, width=30)
lbl_ref_analysis.grid(row=2, column=0, sticky='w', padx=column0_padx)
ref_analysis_lib.grid(row=2, column=1, sticky='w')
# buttons
bottom_frame = Frame(master)
bottom_frame.grid(row=5, column=1, columnspan=3, sticky='w')
btn_start = Button(bottom_frame, text = "Go", width=7)
btn_start.pack(side='left')
btn_commit = Button(bottom_frame, text="Commit", width=7,
command=self.write_file) # added command=
btn_commit.pack(side='left', padx=80)
btn_exit = Button(bottom_frame, text="Exit", width=7)
btn_exit.pack(side='left')
# additional method
def write_file(self):
""" Write data to file. """
with open(filename, 'wt') as outf:
outf.write(...whatever...)
outf.write(...whatever else...)
...
root = Tk()
root.title("Templates")
root.minsize(800, 400)
app = App(root)
root.mainloop()