Search code examples
pythontkintertk-toolkitpython-3.3

Python Tkinter Gui Not Working


I have had an issue with this piece of code from awhile back, it's part of a GCSE mock and I have currently finished the working code (with text only) but I would like to expand it so that it has a nice GUI. I'm getting some issues with updating my sentence variables within the code. Anyone with any suggestions for me please do explain how I can fix it.

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

Solution

  • If I understand your question right, you want to display the entered text in the usersentence label.

    Changing update_sentence() function to what is shown below will archive the desired effect.

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

    usersentence never gets updated because you only set it once when the program starts this was the problem.