1
1

More than 3 years have passed since last update.

pythonで「A と、見せかけて B」と、出力してくるコードをtkinterで動かす

Last updated at Posted at 2020-04-11

目的

pythonで「A と、見せかけて B」と、出力してくるコードをtkinterで動かす際の備忘録です

準備

tkinterは、PythonでGUIを組むことのできるツールです。
以下を参考にさせて頂きます

Python Tkinterのボタンでイベント処理
PythonのTkinterを使ってみる

コード

sample.py
import tkinter as tk
import random

l = ["りんご",
     "みかん",
     "いちご",
     "パイナップル",
     "ドラゴンフルーツ"]

def bot():
    string1=random.choice(l)
    string2=random.choice(l)

    string=string1+" と、見せかけて "+string2

    label = tk.Label(text=string)  # ラベルを定義                                                                    
    x = random.randrange(640)
    y = random.randrange(480)
    label.place(x=x, y=y)                  # ラベルを設置                                                            

window = tk.Tk()                                        #ウィンドウを定義                                            
window.title("Title")                                   #ウィンドウタイトルを設定                                    
window.geometry("640x480")                              #ウィンドウサイズを設定                                      

btn = tk.Button(window, text='と、見せかけて', command = bot)   #ボタンの作成                                        
btn.place(x=10, y=10)

window.mainloop()                                       #メインループとして待機状態にする                    

テスト

ezgif.com-gif-maker.gif

参考

Python Tkinterのボタンでイベント処理
PythonのTkinterを使ってみる

1
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
1
1