在tkinter文本框中插入消息不可见

时间:2019-03-07 08:09:56

标签: python-3.x function tkinter

在让python完成命令并将其插入文本框之前,在继续执行该行代码之前,我一直遇到问题。我使用生成器编写了一个脚本(请参见“工作”功能),我的代码如下:

import tkinter
from tkinter import *
import time


def work(given_string):
    send_receive = yield given_string
    new_string = given_string + " Received, You did it!! :-D"

    yield new_string


def contains_word(s, w):
    return((' ' + w + ' ') in (' ' + s + ' '))


def close_window():
    window.destroy()
    exit()

在下面的函数中调用上述函数。 “ contains_word”函数检查字符串w是否在s中。随后的“ on_click”功能在“ main_fun”提交按钮中被称为命令。

def on_click(output):
    # this generates the object that will be just for the function call:
    result_obj = work(" john ")

    # first function call, until first yield statement:
    result_1 = next(result_obj)

    output.insert(END, result_1)

    time.sleep(3)   # Delays for 3 seconds. You can also use a float value.

    if (contains_word(output.get(1.0, "end-1c"), "john") == TRUE):
        # second function call...
        result_2 = result_obj.send("john sent back to function")
        output.delete(1.10, "end-1c")
        output.insert(END, result_2 +
                      "\n" + "All commands completed")


def main_fun():

    global exit_button, window, output, x_ref, v
    window = tkinter.Tk()
    window.geometry("400x200")  # Width x Height
    v = IntVar()

    output = Text(window, width=40, height=5, wrap=WORD, background="white")
    output.grid(row=5, column=0, columnspan=2, sticky=W)

    submit_button = Button(window, text="SUBMIT", width=6,
                           command=lambda: on_click(output))
    submit_button.grid(row=3, column=0, sticky=W)

    x_ref = 13
    exit_button = Button(window, text="Exit", width=14, command=close_window)
    exit_button.grid(row=x_ref, column=0, sticky=W)

    window.mainloop()


main_fun()

简单地说,我的问题是在执行“ output.insert(END,result_1)”之后,在执行以下if语句的内容之前,它在“输出” tkinter对象中不可见。

我尝试使用时间延迟,但是此操作不起作用,问题仍然存在。

即使是陌生的事实也是if语句的内容执行。因此,文本位于“输出”对象中,但我看不到它。请协助。

0 个答案:

没有答案
相关问题