tkinter按钮命令功能未定义

时间:2014-08-05 16:17:38

标签: python tkinter

我正在尝试从文本框中获取输入并尝试将它们写入文件: 我得到的错误是:retrieve_input未定义。 请帮我纠正我的代码:

编码:

import tkinter as tki 
class App(object):

    def __init__(self,root):
        self.root = root

    # create a Frame for the Text and Scrollbar
        txt_frm = tki.Frame(self.root, width=600, height=400)
        txt_frm.pack(fill="both", expand=True)
        # ensure a consistent GUI size
        txt_frm.grid_propagate(False)   

        self.txt1 = tki.Text(txt_frm, borderwidth=3, relief="sunken", height=4,width=55)
        self.txt1.config(font=("consolas", 12), undo=True, wrap='word')
        self.txt1.grid(row=0, column=1, sticky="nsew", padx=2, pady=2)

        scrollb1 = tki.Scrollbar(txt_frm, command=self.txt1.yview)
        scrollb1.grid(row=0, column=2, sticky='nsew')
        self.txt1['yscrollcommand'] = scrollb1.set

        button = tki.Button(self,text=u"Click                                                                     command=retrieve_input) 
        button.grid(column=1,row=0) 
    def retrieve_input():
        input = self.txt1.get("0.0",'END-1c')
        with open('hello.txt','w') as f:
            f.wite(input)  
root = tki.Tk()
app = App(root)
root.mainloop()

1 个答案:

答案 0 :(得分:1)

除了显而易见的拼写错误之外,问题在于这一行:

button = tki.Button(self,text="Click", command = self.retrieve_input)

请注意,传递给tk.Button的第一个参数是self。第一个参数必须是一个小部件,但你给它自己不是一个小部件。也许您打算使用txt_form?