【问题标题】:Text and Button Python Tk GUI文本和按钮 Python Tk GUI
【发布时间】:2015-04-24 07:03:29
【问题描述】:

如何在 Python Tk GUI 中按下按钮后将插入的文本一一显示?我成功运行了以下程序,但它在最后一个文本的末尾将所有插入的文本一起填充。我想按插入文本的顺序相应地更新文本字段。任何人都可以帮忙吗?谢谢。

from Tkinter import *

root = Tk()
root.geometry("300x400")

def startProgram(shapefile_path):

    t1 = "testing on file 0 successfull\n"
    text.insert('1.0', t1)

    t2 = "testing on file 1 successfull\n" 
    text.insert('1.0', t2)

    t3 = "testing on file2 successfull\n" 
    text.insert('1.0', t3)

text = Text(root, height=8, width=25)
text.grid(row=10, column=0, padx=20, pady=5)

label = Label(root, text="Status",font=12)
label.grid(row=9, column=0, padx=5, pady=5)


button2=Button(root,text="Start Program", width=20,font=12,command=lambda:     startProgram(0))
button2.grid(row=7, column=0, padx=20, pady=10)

button3=Button(root, text='Exit Program',    width=20,font=12,command=root.destroy)
button3.grid(row=8, column=0,padx=20, pady=10)

root.mainloop()

【问题讨论】:

    标签: python button text tkinter


    【解决方案1】:

    据我了解,您想颠倒插入顺序?如果是这样,您可以使用INSERT 代替1.0

    from Tkinter import *
    
    root = Tk()
    root.geometry("300x400")
    
    message_list = ["testing on file 0 successfull\n",
                    "testing on file 1 successfull\n",
                    "testing on file2 successfull\n"]
    
    message_list_idx = 0;
    
    
    def startProgram():
        global message_list_idx
    
        if message_list_idx >= len(message_list):
            return
        t3 = message_list[message_list_idx]
        text.insert('1.0', t3)
        message_list_idx += 1
    
    
    text = Text(root, height=8, width=25)
    text.grid(row=10, column=0, padx=20, pady=5)
    
    label = Label(root, text="Status",font=12)
    label.grid(row=9, column=0, padx=5, pady=5)
    
    
    button2=Button(root,text="Start Program", width=20,font=12,
                   command=startProgram)
    
    button2.grid(row=7, column=0, padx=20, pady=10)
    
    button3=Button(root, text='Exit Program',width=20,font=12,
                        command=root.destroy)
    button3.grid(row=8, column=0,padx=20, pady=10)
    
    root.mainloop()
    button2=Button(root,text="Start Program", width=20,font=12,
                   command=startProgram)
    
    button2.grid(row=7, column=0, padx=20, pady=10)
    
    button3=Button(root, text='Exit Program',width=20,font=12,
                        command=root.destroy)
    button3.grid(row=8, column=0,padx=20, pady=10)
    
    root.mainloop()
    

    【讨论】:

    • 感谢重播,其实插入的顺序没有颠倒,我想按照文本t1,t2,t3的顺序一个一个的更新文本域。当前代码将所有文本一起显示。有什么建议吗?
    • 所以你想在插入文本之前清除文本文件,或者在显示新文本之前引入一些延迟(例如1s)?
    • 我想在按下按钮后在文本字段中相应地打印任何新的文本,就像跟踪程序的过程一样。我试图清除以前插入的文本并显示新的内容,但上面的代码只显示按下按钮操作后的最后一个文本内容,换句话说,它只显示最后插入的文本而不是一个接一个地显示一个文本,即使你在两个文本之间放置时间延迟。
    • @HotPat1000 对不起,我还是不明白。也许您可以修改您的问题以提供更多信息、一些示例输入、屏幕截图等。
    • @Marcin 感谢您的耐心。让我换个说法,我真正想做的是,当我在 GUI 中按下“开始程序”按钮时,“startProgram”函数应该运行并逐个显示文本,例如先显示文本 1,然后显示文本 2第二个也是最后一个是显示文本 3。但是,我上面写的代码并没有做这项工作,它只在运行函数“startProgram”后显示所有文本一次。所以我的问题是如何在函数内部按顺序一一显示文本(t1,t2,t3)。如果您可以运行上面的代码,您可能会更清楚。谢谢。
    猜你喜欢
    • 2015-07-27
    • 2014-12-07
    • 1970-01-01
    • 2021-05-20
    • 1970-01-01
    • 2011-10-25
    • 2020-11-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多