Suddenly I decided to make my own original application with python, so I tried using tkinter. However, I had a hard time understanding what to do when the button was pressed, so I wrote this article. If you don't know how to write the process when a button is pressed in tkinter, or if you want to know how to change the contents of the label, please read it.
#Load the required libraries
import tkinter as tk
import random
#Screen settings
root = tk.Tk()
root.title("Dice.app")
root.geometry('200x150')
#Processing when the button is pressed
def dice(event):
#Generate a random integer and rewrite the contents of label
value["text"] = random.randint(1,6)
value = tk.Label(text="0",font=("",80))
value.pack(fill = 'x', padx=20, side = 'top')
#Button settings
Btn = tk.Button(text='Roll the dice',width=10)
#Tie a button and a trigger
Btn.bind("<Button-1>", dice)
#Plot the button
Btn.pack(fill = 'x', padx=20, side = 'top')
root.mainloop()
Recommended Posts