How to update the Tkinter window?

Asked

Viewed 368 times

0

I made a program that plays dice. I used the graphical library Tkinter to create buttons and Labels. I use the button to activate a function that generates a random number, but the label does not change text, IE, the result of the data does not appear. Does anyone know how I proceed? Follow the code:

import tkinter
import tkinter.font
from random import randint



class Dice():



    def __init__(self):



        self.window = tkinter.Tk()




    def random_number(self):
        self.result = str(randint(0,100))   



    def elements(self):

        self.result = 'Resultado do dado'
        self.font = tkinter.font.Font(family='Arial', size=20)


        self.texto = tkinter.Label(text=self.result, width=20, height=2, font=self.font)
        self.cem = tkinter.Button(text='D100', width=10, height=2,font=self.font, command=self.random_number)
        self.quatro = tkinter.Button(text='D4', width=10, height=2,font=self.font)
        self.seis = tkinter.Button(text='D6', width=10, height=2,font=self.font)
        self.oito = tkinter.Button(text='D8', width=10, height=2,font=self.font)


        self.texto.grid(columnspan=2, row=0)
        self.cem.grid(column=0, row=1)
        self.quatro.grid(column=1, row=1)
        self.seis.grid(column=0, row=2)
        self.oito.grid(column=1, row=2)








dado = Dice()
dado.elements()

1 answer

2


For Tkinter to do something that is visible, you have to pass the control to it - after performing the functions and methods that build your window, add the initial data, etc, you do this by calling tkinter.mainloop() - in that code, that call would be just below the call to dado.elements().

After calling the tkinter.mainloop() the only code of yours that will run will be whatever is connected to Tkinter events - in case the method Dado.random_number - which is passed as the command boot self.cem (none of the other buttons have a command nor is read - will do nothing).

but then -- your .ramdom_number have to do more things if he changes only the self.result - this is an internal string of your class that is not connected to Tkinter - if you want to see something in the interface, you have to make the call to update the self.text - it can simply be the line

self.texto["text"] = str(self.resultado)  

within the method random_number.


Do these two things first - once you see them working, it’ll be a lot easier to understand how the Tkinter app works - it’s quite different from using print and input - Then you add the values to the command of the other buttons. To not write a lot of methods that do almost anything, you can use the "lambda" feature when writing a command -

For example, instead of random_number generate a fixed number between 0 and 100 (do you have a data that gives "0"??), it can accept a parameter, telling which data to use, and you use the lambda to pass this parameter - if you do not do this, you will have to have a method for each data type:

    ...
    def random_number(self, lados):
        self.result = str(randint(1, lados))   
        self.texto["text"] = self.result

    def elements(self):

        self.result = 'Resultado do dado'
        self.font = tkinter.font.Font(family='Arial', size=20)

        self.texto = tkinter.Label(text=self.result, width=20, height=2, font=self.font)
        self.cem = tkinter.Button(text='D100', width=10, height=2,font=self.font, command=lambda: self.random_number(100))
        self.quatro = tkinter.Button(text='D4', width=10, height=2,font=self.font, command=lambda: self.random_number(4))
        self.seis = tkinter.Button(text='D6', width=10, height=2,font=self.font, command=lambda: self.random_number(6))
        self.oito = tkinter.Button(text='D8', width=10, height=2,font=self.font, command=lambda: self.random_number(8))

...
dado = Dice()
dado.elements()
tkinter.mainloop()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.