文本和按钮Python Tk GUI

时间:2015-02-23 01:17:52

标签: python button text tkinter

如何在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()

1 个答案:

答案 0 :(得分:0)

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

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()