First program with GUI: Roll a dice

mathewchan

HHMathewChan

Posted on June 20, 2022

First program with GUI: Roll a dice

This times is to record a memorable day for me to create a program which look closer to what will be usable in real life.

Code

This program is to act like a 6-face dice, when you press the button, a number will be randomly picked from 1 to 6 and display it

import random
import tkinter as tk


def random_a_num():
    value = int(random.randrange(1, 7))
    lbl_value["text"] = f"{value}"


window = tk.Tk()

window.rowconfigure([0, 1], minsize=50, weight=1)
window.columnconfigure(0, minsize=50, weight=1)

btn_roll = tk.Button(master=window, text="Roll", command=random_a_num)
btn_roll.grid(row=0, column=0, sticky="nsew")

lbl_value = tk.Label(master=window, text="0")
lbl_value.grid(row=1, column=0, sticky="nsew")

window.mainloop()
Enter fullscreen mode Exit fullscreen mode

A program with one button and one label

Reflection

I have learned a lot of useful basic syntax and concept in tkinter module. When the interface comes out and it works, I feel like my excitement on learning coding has increase a lot. More confidence to go on the learning path.

💖 💪 🙅 🚩
mathewchan
HHMathewChan

Posted on June 20, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related