更改后如何获取tkinter文本框文本?

时间:2018-09-02 22:59:28

标签: python tkinter

这是使用全局变量的适当时间吗?我应该使用stringVar吗?用户更改其值后,我该怎么办?

当我在print(my_msg)函数的my_msg = sms_textbox.get('1.0', 'end-1c')行下方添加get_txtmsg()语句时,它将打印文本已更改。

print(my_msg)函数中相同的askfilename_sms()语句返回"Hi, how are you?"

    def askfilename_sms():
        print(my_msg)

    ent3 = ttk.Labelframe(
        tab1, text='New Client Survey SMS')
    ent3.grid(
        row=3, column=0, sticky=tk.NSEW, padx=2, pady=2)

    ttk.Label(
        ent3, text='This....\n').grid(row=0, column=-0)

    def get_txtmsg():
        global my_msg
        my_msg = sms_textbox.get('1.0', 'end-1c')
        askfilename_sms()

    my_msg = "Hi, how are you?"

    sms_btn = ttk.Button(
        ent3, text="Send SMS Surveys",
        command=lambda: get_txtmsg())
    sms_btn.grid(
        row=1, column=1, padx=90, pady=10)
    sms_textbox = tk.Text(
        ent3, height=6, width=34)
    sms_textbox.insert(
        '1.0', my_msg)
    sms_textbox.grid(
        row=0, column=1)

编辑:

所以我不太确定为什么会这样,但是我将所有功能都整合到一个功能中,现在就可以了(虽然不高兴,但也许是正确的方法):

    def get_txtmsg():
        global my_msg
        my_msg = sms_textbox.get('1.0', 'end-1c')
        sms_msg_box = tk.messagebox.askyesno(
            title='Are you ready to begin?',
            message='click to send')
        if sms_msg_box:
            tk.messagebox.showinfo(
                message='Please select New Client Report')
            sms_nc_filename = filedialog.askopenfilename(
                title="Select New Client file",
                filetypes=(("excel files", "*.xls"), ("all files", "*.*")))
            if not sms_nc_filename:
                return
            else:
                send_sms.send_survey_text(sms_nc_filename, my_msg)

    my_msg = "Hi, how are you"

    sms_btn = ttk.Button(
        ent3, text="Send SMS Surveys",
        command=lambda: get_txtmsg())
    sms_btn.grid(
        row=1, column=1, padx=90, pady=10)
    sms_textbox = tk.Text(
        ent3, height=6, width=34)
    sms_textbox.insert(
        '1.0', my_msg)
    sms_textbox.grid(
        row=0, column=1)

0 个答案:

没有答案