Lesson-7: Generating Numbers By Pressing The tkinter Button

Generating Numbers By Pressing The tkinter Button

import tkinter as tk
import random

pencere=tk.Tk()
def uret():
    liste = []
    while len(liste) != 6:
            a = random.randint(1, 100)
            if a not in liste:
                liste.append(a)

    etiket1["text"] = liste


pencere.title("Python Tkinter Lessons")
pencere.geometry("200x200+50+100")
pencere.resizable(width="FALSE", height="FALSE")

etiket1=tk.Label(pencere,fg="red",text="To generate press button...")
etiket1.pack()
buton1=tk.Button(pencere,text="Generate...",command=uret)
buton1.pack()

pencere.mainloop()

In the example above, our window has one label and one button. When this button is pressed, it directs us to the Generate function (command=generate) the command Command determines what to do when we click on the button. then let’s examine the uret function:

def uret():
    liste = []
    while len(liste) != 6:
            a = random.randint(1, 100)
            if a not in liste:
                liste.append(a)

    etiket1["text"] = liste

This function gives us random numbers from 1 to 100. With the For loop, we add 6 numbers that are not a list to a 6 Loop (liste.append (a)).

EKRAN GÖRÜNTÜSÜ:

Buton ile rastgele sayı üretme
Generating Numbers By Pressing The Button

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *