LoginSignup
2
1

More than 3 years have passed since last update.

tkinterでサイコロを作ろう

Last updated at Posted at 2021-01-01

始めに

ふと自分のオリジナルアプリをpythonで作ってみようと思い、tkinterを使ってみました。しかし、ボタンの押された時の処理がなかなか分からず苦労したので記事にしました。tkinterでボタンが押された時の処理をどう書けばいいのか分からない人や、ラベルの内容の変更方法の知りたい方は読んで下さい。

ソースコード

# 必要なライブラリーを読み込む
import tkinter as tk
import random

# 画面の設定
root = tk.Tk()
root.title("Dice.app")
root.geometry('200x150')

# ボタンが押された時の処理
def dice(event):
    # ランダムな整数を生成して、labelの内容を書き換える
    value["text"] = random.randint(1,6)


value = tk.Label(text="0",font=("",80))
value.pack(fill = 'x', padx=20, side = 'top')


# ボタンの設定
Btn = tk.Button(text='サイコロをふる',width=10)
# ボタンとトリガーを紐ずける
Btn.bind("<Button-1>", dice)
# ボタンをプロット
Btn.pack(fill = 'x', padx=20, side = 'top')

root.mainloop()

参考文献

サイト

2
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1