-2

I am trying to get a label to display a random number when a button is clicked. I have tried get(), set() and config() in the function, as per similar cases on stackoverflow but to no avail. Where do I go wrong?

import tkinter as tk
import random

HEIGHT=200            #Window height
WIDTH=300             #Window width
TITLE="Random number" #Window title

LWIDTH=40             #Label width
LHEIGHT=50            #Label height
LFONTSIZE=44          #Label font size

def buttonpress():
    LTEXT.set(random.randint(0, 100)) #The intention is for a new value to be calculated
    l.text=LTEXT                      #The intention is for the label text to be updated. Have tried set(), get(), config()

BUTTONWIDTH=17    #button width
BUTTONHEIGHT=2    #button height, but in rows instead of pixels (!!!)

root=tk.Tk()
root.title(TITLE)

LTEXT=tk.IntVar(root)               #defining the intvar
LTEXT.set(random.randint(0, 100))   #setting the initial value

f = tk.Frame(root,width=WIDTH,height=HEIGHT)
f.pack()

l=tk.Label(width=LWIDTH, height=LHEIGHT, text=LTEXT.get(),font=(None,LFONTSIZE))
l.place(relx=0.5, rely=0.3, anchor="center")

b=tk.Button(root,width=BUTTONWIDTH, height= BUTTONHEIGHT, text = "New number",command=buttonpress())
b.place(relx=0.5, rely=0.7, anchor="center")

root.mainloop()
  • 1
    ***`command=buttonpress()`***: Read [why-is-button-parameter-command-executed-when-declared](https://stackoverflow.com/questions/5767228) – stovfl May 26 '20 at 18:25

2 Answers2

3

To get the text form your label : print(mylabel["text"])

So to modify it : mylabel["text"] = myrandomnumber

It works with every parameter, for everything, buttons, labels, canvas, etc...

Exemple :

from tkinter import *
root = Tk()
label = Label(text="hello")
def change():
    label["text"] = "world"
button = Button(text="Change", command=change)
label.pack()
button.pack()
Quantum Sushi
  • 504
  • 7
  • 23
  • Thank you for your reply. I tried doing putting brackets after my label (also previously), but instead of a random number I get a label that says: "PY_VAR0". So I still didn't get the label to update. – HarryBarry May 26 '20 at 20:30
  • @HarryBarry check the edit I made, I added a small code that actually works for me with that method – Quantum Sushi May 27 '20 at 06:22
  • Thank you! Using the code you sent, I found the problem: i had left the parentheses "()" when in the function name in the command field. So I guess it wasn't calling and running the function. When I removed the parentheses, new numbers were showing, with each button click. Apologies for my mundane mistake - I really appreciate the trouble you went through and your help! – HarryBarry May 27 '20 at 08:17
  • Hahaha, it happened to me some times, don't worry ! ^^ And you're welcome, that's why we're all here ! – Quantum Sushi May 27 '20 at 08:24
0

I am trying to get a label to display a random number when a button is clicked. I have tried get(), set() and config() in the function.

Remove parenthesis bracket on Button command keyword.

There are two options to choose from.

  1. l.config(text=LTEXT.get())
  2. l['text']=LTEXT.get()

Snippet:

Option 1:

def buttonpress():
    l['text']=LTEXT.get()

Option 2:

def buttonpress():
    LTEXT.set(random.randint(0, 100))  

Screenshot:

enter image description here

toyota Supra
  • 3,181
  • 4
  • 15
  • 19