Python Tkinter Gui 不工作

Python Tkinter Gui Not Working

一段时间前我对这段代码有疑问,它是 GCSE 模拟的一部分,我目前已经完成了工作代码(只有文本),但我想扩展它以便它有一个漂亮的图形用户界面。我在代码中更新句子变量时遇到了一些问题。任何对我有任何建议的人请解释我如何解决它。

    #GCSE TASK WITH GUI
    import tkinter
    from tkinter import *
    from tkinter import ttk

    var_sentence = ("default")

    window = tkinter.Tk()
    window.resizable(width=FALSE, height=FALSE)
    window.title("Sentence")
    window.geometry("400x300")
    window.wm_iconbitmap("applicationlogo.ico")

    file = open("sentencedata.txt","w")
    file = open("sentencedata.txt","r")

    def update_sentence():
        var_sentence = sentence.get()


    def submit():
        file.write(sentence)
        print ("")

    def findword():
        messagebox.showinfo("Found!")
        print ("Found")


    sentencetext = tkinter.Label(window, fg="purple" ,text="Enter Sentence: ")
    sentence = tkinter.Entry(window)
    sentencebutton = tkinter.Button(text="Submit", fg="red" , command=update_sentence)

    findword = tkinter.Label(window, fg="purple" ,text="Enter Word To Find: ")
    wordtofind = tkinter.Entry(window)
    findwordbutton = tkinter.Button(text="Find!", fg="red" ,command=findword)


    usersentence = sentence.get()
    usersentence = tkinter.Label(window,text=sentence)


    shape = Canvas (bg="grey", cursor="arrow", width="400", height="8")
    shape2 = Canvas (bg="grey", cursor="arrow", width="400", height="8")

    #Packing & Ordering Moduales
    sentencetext.pack()
    sentence.pack()
    sentencebutton.pack()

    shape.pack()

    findword.pack()
    wordtofind.pack()
    findwordbutton.pack()
    usersentence.pack()

    shape2.pack()

    window.mainloop()

如果我理解你的问题,你想在 usersentence 标签中显示输入的文本。

update_sentence() 函数更改为如下所示将存档所需的效果。

def update_sentence():
    var_sentence = sentence.get()
    usersentence.config(text=var_sentence)

usersentence 永远不会更新,因为您只在程序启动时设置了一次,这就是问题所在。