0

I have a variable that is updated on a button click, and I want to show the variable's value in a Text widget, but I'm getting a blank every time despite checking that the variable is not empty.

I'm using a StringVar called var as a middle man but the result is still blank.

from tkinter import *
import time

var = None

def get_tags():
    tag_info={'hashtags':"Loading..."}
    var.set(str(tag_info.get('hashtags','')))

if __name__ == '__main__':
   root = Tk()
   root.title("Tag Generator")
   var = StringVar(root)
   show_button = Button(root, text='Show',
          command=lambda: get_tags())
   show_button.grid(column=2,columnspan=1, padx=5, pady=5, row=num+1)

   result_text = Text(root)
   result_text.grid(column=0,columnspan=3, padx=5, pady=5, row=num+2, rowspan=4)
   result_text.insert('1.0',var.get())

   while True:
       time.sleep(0.01)
       root.update()

How can I show the contents of var in my Text widget?

EDIT:contents of my var variable:

var.get()
Out[86]: '.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n#cat #dog #catsofinstagram #cats #pet #dogsofinstagram #catlover #cute #love #dogs #pets #catstagram #instagood #puppy #kitty #animals #petstagram #dogstagram #gato #kitten #animal #instadog #catoftheday #doglover #cats_of_instagram #adorable #instagram #dogoftheday #instacat #meow '

(which is as expected/desired)

5
  • I added the necessary lines to your code to make it runnable and it seems like there is no problem. var.get() is adding text into the Text widget it could've to do with tag_info try printing it to the console and check if it has any value or not? Commented May 9, 2019 at 14:55
  • var.get() run alone seems fine, I edited my question to demonstrate Commented May 9, 2019 at 15:02
  • I think the issue is that the contents of the Text widget doesn't update Commented May 9, 2019 at 15:13
  • We can't run your code, it has many problems. Commented May 9, 2019 at 15:15
  • @BryanOakley Thank you for your enlightening comment. It appears I forgot to add the imports and initial declaration of var. I will edit my question accordingly and humbly beg your forgiveness Commented May 9, 2019 at 15:19

1 Answer 1

2

The text is not showing because the var = '' at line result_text.insert('1.0',var.get())and you can't press the Button before mainloop so by putting the same line in function get_tags() should solve the issue.

But at this point I'm wonder why you even need var. If you're not using it for anything else then you should remove it and insert in the Text directly.

def get_tags():
    tag_info={'hashtags':"Loading..."}
    tag_info = my_file.get_tags()
    var.set(str(tag_info.get('hashtags','')))
    result_text.insert('1.0', var.get())
Sign up to request clarification or add additional context in comments.

3 Comments

I added var because I had the same issue when I directly put the text in, and assumed it was a format issue
Do you want the text in a straight line? As the string have so many \n which trends text to go to next line..
No, that's normal for the expected output, and your fix here works perfectly

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.