samedi 16 janvier 2021

update label variable in python tkinter

from tkinter import *
import tkinter as tk
import random

f = 0

def test_print():
    f = random.randint(0,118)
    master.update_idletasks()

# creating Tk window
master = Tk()

master.minsize(600,400)
 
var = IntVar()
var.set(f)

# cretaing a Fra, e which can expand according
# to the size of the window
pane = Frame(master)
pane.pack(fill = BOTH, expand = True)
 
# button widgets which can also expand and fill
# in the parent widget entirely
# Button 1
b1 = Button(pane, text = "Click me !", 
            background = "red", fg = "white", command = test_print)
b1.pack(side = TOP, expand = True, fill = BOTH)
 
label = tk.Label(pane, textvariable = var)
label.pack(side = BOTTOM, expand = False)

# Execute Tkinter
master.mainloop()

This code runs fine but it doesn't give me a random number when i press the button. I need it to give me a random output so i can get a random element from a list. Does anybody have the answer to this?




Aucun commentaire:

Enregistrer un commentaire