Tkinter Label not showing Int variable

2024/10/11 10:24:49

I'm trying to make a simple RPG where as you collect gold it will be showed in a label, but it doesn't!! Here is the code:

def start():Inv=Tk()gold = IntVar(value=78)EtkI2=Label(Inv, textvariable=gold).pack()

I'm new to python and especially tkinter so I need help!!!

Answer

The only thing wrong with your code is that you aren't calling the mainloop method of the root window. Once you do that, your code works fine.

Here's a slightly modified version that updates the value after 5 seconds:

from Tkinter import *def start():Inv = Tk()Inv.geometry("200x200")gold = IntVar(value=78)EtkI2=Label(Inv, textvariable=gold).pack()# chanage the gold value after 5 secondsInv.after(5000, gold.set, 100)# start the event loopInv.mainloop()start()

There are some other things that could be improved with your code. For exaple, EtkI2 will be set to None since that is what pack() returns. It's best to separate widget creation from widget layout. Also, it's better to not do a global import (from Tkinter import *). I recommend import Tkinter as tk ... tk.Label(...).

I explain more about that along with using an object-oriented approach here: https://stackoverflow.com/a/17470842

https://en.xdnf.cn/q/118334.html

Related Q&A

How to refer a certain place in a line in Python

I have this little piece of code:g = open("spheretop1.stl", "r") m = open("morelinestop1.gcode", "w") searchlines = g.readlines() file = "" for i, line…

List comprehension output is None [duplicate]

This question already has answers here:Python list comprehension: list sub-items without duplicates(6 answers)Closed 8 years ago.Im new to python and I wanted to try to use list comprehension but outco…

error while inserting into mysql from python for loop [duplicate]

This question already has answers here:Closed 12 years ago.Possible Duplicate:convert list to string to insert into my sql in one row in python scrapy Is this script correct. I want to insert the scra…

Half of Matrix Size Missing

I wanted to ask why is it that some of my matrices in Python are missing a size value? The matrices/arrays that re defined show the numbers in them, but the size is missing and it is throwing me other…

Clicking Side Panel Elements in Selenium Without IFrames

I want to download U.S. Department of Housing and Urban Development data using Pythons Selenium. Heres my code. import os from selenium import webdriver from webdriver_manager.chrome import ChromeDrive…

Library to extract data from open Excel workbooks

I am trying to extract data from workbooks that are already open. I have found the xlrd library, but it appears you can only use this with workbooks you open through Python. The workbooks I will use in…

Keras apply different Dense layer to each timestep

I have training data in the shape of (-1, 10) and I want to apply a different Dense layer to each timestep. Currently, I tried to achieve this by reshaping input to (-1, 20, 1) and then using a TimeDis…

Create a pass-through for an installed module to achieve an optional import

Im writing a library in python 3.7. Id like it to have as few dependencies as possible. For example, tqdm is nice to have but ideally Id like my library to work without it if its not there. Therefore, …

Django, Redis: Where to put connection-code

I have to query redis on every request in my Django-app. Where can I put the setup/ connection routine (r = redis.Redis(host=localhost, port=6379)) so that I can access and reuse the connection without…

Events and Bindings in tkinter does not work in loop

I am trying to create binding in a loop using tkinter module.from tkinter import * class Gui(Frame):def __init__(self, parent):Frame.__init__(self, parent) self.parent = parentself.initUI()def Arrays(…