2

I have made a variable called 'localtime2' within my def in the code and then have a label which says 'textvariable=localtime2.' the problem is that it does not display the information about the variable.

localtime2 = time.asctime(time.localtime(time.time()))
tk.Label(roots, font=('arial', 16, 'bold'), textvariable=localtime2, bd=16, anchor="w").grid(row=2, column=0)

This is all I have in the code about this variable and it is not coming up with any error in the terminal. It just doesnt show at all.

5
  • 1
    Can you share the complete code? Commented Aug 21, 2018 at 8:45
  • Would it be ok if i put on a github post and linked Commented Aug 21, 2018 at 8:45
  • gist.github.com/SJones72413/f4ef37cacdf49bd9a7d4a890bcfb7d75 link for entire code Commented Aug 21, 2018 at 8:46
  • The variable might not be able to keep up as the time is always moving, maybe you could put in a while True: loop? Commented Aug 21, 2018 at 9:28
  • I have used time previously as i have 2 gui's in the code and have done the exact same thing in the other one however it just doesnt work in this gui. Thank you for taking the time to reply. Commented Aug 21, 2018 at 9:29

2 Answers 2

9

Edit: The solution to the original post was using text=localtime2.get() instead of textvariable=localtime2 in the label widget (for some strange reason). However, my original answer is still correct as tkinter variables should be used and so I will keep it up.


You must use tkinter variables in tkinter widgets and not normal python variables. Tkinter variables are slightly different to normal variables and must first be defined as a specific data type. For example, a variable which contains a string must be first created like so:

string_variable = tk.StringVar()

likewise a boolean would be created like so:

boolean_variable = tk.BooleanVar()

Read here for more information on tkinter variables.

These variables are in fact classes so in order to set them use must call the .set() method. Therefore to set a tkinter String variable to "a string" you would use the following code:

string_variable = tk.StringVar() # Create the variable 
string_variable.set("a string") # Set the value of the variable

Thus to fix your code you need to make localtime2 a tkinter variable and set it to the time using the .set() method.

Example:

localtime2 = tk.StringVar() # Create the localtime2 string variable
localtime2.set(time.asctime(time.localtime(time.time()))) # Set the variable
tk.Label(roots, font=('arial', 16, 'bold'), textvariable=localtime2, bd=16, anchor="w").grid(row=2, column=0)
Sign up to request clarification or add additional context in comments.

7 Comments

I really do appreciate the time you have taken do do this however it has not fixed the problem. It still doesnt have a error message also it just does not show still.
May I see the rest of the code. The github link you provided in the comments brings me to a 404 page (page not found)
I am quite new to python so it might be a complete and utter mess. Its not that I do it on purpose i just havent learnt the ways to avoid it yet.
@Jsam This is very odd. It seams that the label will not not work using the textvariable field. However it seemed to display when i used the text field instead. Use this code instead text=localtime2.get() in your Label widget that uses the localtime2 variable and see if that works.
@Jsam Glad to help! Although I have no idea why that works and not the other way. ;)
|
3

Whenever there is a change in a tkinter variable, the update is automatically reflected everywhere. Because of this property you cannot use a normal python variable here.
Try using StringVar() and then setting the variable content using set()

1 Comment

Hi. Thank you for taking the time. I have just tried this as the person above has mentioned however it has not seemed to change anything. Thank you

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.