[PYTHON] Let's make dice with tkinter

At the beginning

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.

Source code

#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()

References

Site

Recommended Posts

Let's make dice with tkinter
Let's make Othello with wxPython
Let's make a Mac app with Tkinter and py2app
Let's make a GUI with python.
Let's make a breakout with wxPython
Let's make Othello AI with Chainer-Part 1-
Let's make a graph with python! !!
Let's make a supercomputer with xCAT
Let's make Othello AI with Chainer-Part 2-
Let's make a shiritori game with Python
Make GUI apps super easy with tkinter
MVC with Tkinter
Let's make a voice slowly with Python
Let's make a simple language with PLY 1
[Python] Let's make matplotlib compatible with Japanese
Let's make a web framework with Python! (1)
Let's make a tic-tac-toe AI with Pylearn 2
Let's make a Twitter Bot with Python!
Let's make a web framework with Python! (2)
Let's replace UWSC with Python (5) Let's make a Robot
Make Lambda Layers with Lambda
Make Yubaba with Discord.py
Become Santa with Tkinter
Make slides with iPython
[Let's play with Python] Make a household account book
Let's make a simple game with Python 3 and iPhone
Let's make dependency management with pip a little easier
Let's make a spherical grid with Rhinoceros / Grasshopper / GHPython
[Super easy] Let's make a LINE BOT with Python.
Make an application using tkinter an executable file with cx_freeze
Let's make a websocket client with Python. (Access token authentication)
Programming with Python and Tkinter
Let's play with 4D 4th
Let's make a Discord Bot.
Let's play with Amedas data-Part 1
Getting Started with Tkinter 2: Buttons
Make sci-fi-like buttons with Kivy
Let's run Excel with Python
Let's make an Errbot plugin
Let's play with Amedas data-Part 4
Screen switching / screen transition with Tkinter
Create Image Viewer with Tkinter
Make a fortune with Python
Let's write python with cinema4d.
Run Label with tkinter [Python]
Let's do R-CNN with Sklearn-theano
Make HTTPS for free with Amazon Linux2 + Freenom + Let's Encrypt
Let's play with Amedas data-Part 3
I measured BMI with tkinter
Let's play with Amedas data-Part 2
I tried to make GUI tic-tac-toe with Python and Tkinter
Let's make a rock-paper-scissors game
Let's build git-cat with Python
Let's make a diagram that can be clicked with IPython
Make a fire with kdeplot
Make Slack chatbot with Errbot
Let's make a WEB application for phone book with flask Part 1
Let's make a cycle computer with Raspberry Pi Zero (W, WH)
Let's make a WEB application for phone book with flask Part 2
Make a Spinbox that can be displayed in Binary with Tkinter
Let's make a WEB application for phone book with flask Part 3