2

The problem:

I am trying to update the same text widget box from a function that contains some text. Instead a whole new text window appears every time.

Here is my code:

from tkinter import *
import os

#Tkinter graphics

homepage = Tk()
homepage.title("My first GUI")
# set size of window
homepage.geometry('1200x400')
# Add image file 
bg = PhotoImage(file = "maxresdefault.png") 
    
# Show image using label 
label1 = Label( homepage, image = bg) 
label1.place(x = 0, y = 0) 

label2 = Label( homepage, text = "Test App") 
label2.pack() 

# Create Frame 
frame1 = Frame(homepage) 
frame1.pack()

#button initatiors
def buttonupdate():
    S = Scrollbar(homepage)
    T = Text(homepage, height=100, width=30)
    T.pack()
    T.pack(side=RIGHT, fill= Y)
    S.pack(side = RIGHT, fill = Y)
    S.config(command=T.yview)
    T.insert(END, "test")
    T.config(yscrollcommand=S.set, state=DISABLED)


    

# Static buttons
tickets30button = Button(text = "This is button 1", command=buttonupdate) 
tickets30button.place(x=0, y=26) 

mcibutton = Button(text = "This is button 2") 
mcibutton.place(x=0, y=52)

hdebutton = Button(text = "This is button 3")
hdebutton.place(x=0, y=78)

homepage.mainloop()

Here is the result if I click on the first button three times:

Let me know if you have any suggestions that I can try.

Thank you for your time,

6
  • 2
    You create a new Text widget every time the function is called. Move first 5 lines from the function outside the function and it should work Commented Mar 11, 2021 at 17:36
  • 2
    Also why are you calling T.pack() and then T.pack(side=RIGHT, fill= Y)? Just have the T.pack(side=RIGHT, fill= Y). Commented Mar 11, 2021 at 17:37
  • @TheLizzard that totally worked. I deleted the T.Pack() and took out the first five lines from the function and put it above the function and it works! The text widget will appear before I click the button now but that is cool with me if it doesn't keep making new windows every time. lol Thanks again. Commented Mar 11, 2021 at 17:46
  • 1
    You can delete this question or answer it or @TheLizzard can answer it. Commented Mar 11, 2021 at 18:42
  • @CoolCloud In my opinion it isn't good practise to delete the question. Other people might have the same problem and stumble upon this question and find a solution even if it is in the comments. Commented Mar 11, 2021 at 18:50

1 Answer 1

1

I was able to update my text window instead of create a new one, upon each click of a button, thanks to @TheLizzard.

He mentioned to move the section of code that creates the text window outside of the function and keep the section of code that creates the text, inside the function.

Before:

#button initiators
def buttonupdate():
    S = Scrollbar(homepage)
    T = Text(homepage, height=100, width=30)
    T.pack()
    T.pack(side=RIGHT, fill= Y)
    S.pack(side = RIGHT, fill = Y)
    S.config(command=T.yview)
    T.insert(END, "test")
    T.config(yscrollcommand=S.set, state=DISABLED)

After: (UPDATED)

S = Scrollbar(homepage)
T = Text(homepage, height=100, width=30)
T.pack(side=RIGHT, fill= Y)
S.pack(side = RIGHT, fill = Y)
S.config(command=T.yview)
T.config(yscrollcommand=S.set, state=DISABLED)

#button initatiors
def myTicketstatusbutton():
    T.delete(1.0,END)
    T.insert(END, "test")
    
Sign up to request clarification or add additional context in comments.

3 Comments

Also you can move the S.config(command=T.yview) and the T.config(yscrollcommand=S.set, state=DISABLED) outside of your function to make it faster.
Also the T.pack_forget() isn't doing anything so you can remove it (it will have no effect on the actual code)
Ah yes, I forgot to remove that T.pack_forget() . Thanks for catching that. Also I changed the answer to reflect the new information. Thanks again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.