Pythonでウィンドウを表示
example06-02-01
#coding:utf-8
import tkinter as tk
root=tk.Tk() #ウィンドウをつくる
root.mainloop() #ウィンドウを表示
- as tk にしたのはtkinterの省略
- rootという変数を使ってさまざまなウィンドウ操作ができる
ウィンドウサイズを変更
example06-02-02
#coding:utf-8
import tkinter as tk
root=tk.Tk()
root.geometry("400x150")
root.mainloop()
- geometryで「横幅x高さ」を設定
ウィンドウタイトルの設定
example06-02-03
#coding:utf-8
import tkinter as tk
root=tk.Tk()
root.geometry("400x150")
root.title("数当てゲーム")
root.mainloop()
- root.title()で設定できる
メッセージと入力欄の配置
example06-03-01
#coding:utf-8
import tkinter as tk
root=tk.Tk()
root.geometry("400x150")
root.title("数当てゲーム")
labell=tk.Label(root,text="数入れてね") #文字の設定
labell.place(x=20,y=20) #文字を置く場所
root.mainloop()
- ラベルの生成と配置は別々で設定
- ウィンドウの左上が(0,0)座標、そこから広がる
example06-03-02
#coding:utf-8
import tkinter as tk
root=tk.Tk()
root.geometry("400x150")
root.title("数当てゲーム")
labell=tk.Label(root,text="数入れてね")
labell.place(x=20,y=20)
editbox1=tk.Entry(width=4)
editbox1.place(x=120,y=20)
root.mainloop()
- 入力欄を設置→Entry メソッドを使う
- width=数字 は入力欄の幅
字の形(font)の設定
example06-03-03
#coding:utf-8
import tkinter as tk
root=tk.Tk()
root.geometry("400x150")
root.title("数当てゲーム")
labell=tk.Label(root,text="数入れてね",font=("Helvetica,28"))
labell.place(x=20,y=20)
editboxl=tk.Entry(width=4,font=("Helvetica,28"))
editboxl.place(x=120,y=60)
root.mainloop()
- font=()で設定
- Times=明朝体っぽい, Helvetica=ゴシック体っぽい, Courier=等幅タイプライタっぽい
- font("種類",数字) 文字サイズを変更できる
ボタンの作成
example06-04-01
#coding:utf-8
import tkinter as tk
root=tk.Tk()
root.geometry("400x150")
root.title("数当てゲーム")
labell=tk.Label(root,text="数入れてね",font=("Helvetica,28"))
labell.place(x=20,y=20)
editboxl=tk.Entry(width=4,font=("Helvetica,28"))
editboxl.place(x=120,y=60)
button1=tk.Button(root,text="チェック",font=("Helvetica"))
button1.place(x=220,y=60)
root.mainloop()
- 文字通りButtonを使う
- ラベルと同じ使い方
ボタンが押された時の実行と表示
example06-04-01
#coding:utf-8
import tkinter as tk
import tkinter.messagebox as tmsg #省略
#ボタンがクリックされたと時の処理
def ButtonClick():
#タイトル, 内容
tmsg.showinfo("テスト","クリックされたよ")
#メインプログラム
root=tk.Tk()
root.geometry("400x150")
root.title("数当てゲーム")
labell=tk.Label(root,text="数入れてね",font=("Helvetica,28"))
labell.place(x=20,y=20)
editboxl=tk.Entry(width=4,font=("Helvetica,28"))
editboxl.place(x=120,y=60)
#ボタンの設置と実行
button1=tk.Button(root,text="チェック",font=("Helvetica"),command=ButtonClick)
#command はクリックされたとき実行する関数
button1.place(x=220,y=60)
root.mainloop()
- メッセージ表示のためmessageboxを導入
- ボタン表示のところにcommand関数を用いて実行
- commandで実行する関数を事前に設定して置くと見やすい
- tkinter.messageの関数:
- showinfo- 情報表示
- showwarning- 警告表示
- showerror- エラー表示
- askquestion- テキストボックスのメッセージ表示+文字入力
- askokcancle- [OK]と[Cancle]のボタンを持って表示
- askyesno- [Yes]と[No]のボタンを持って表示
- askretrycancle- [Retry]と[Cancle]のボタンを持って表示
ヒット&ブローの判定
example06-05-01
#coding:utf-8
import tkinter as tk
import tkinter.messagebox as tmsg
#ボタンがクリックされたと時の処理
def ButtonClick():
#入力欄の文字を取得
b=editboxl.get()
#それを表示
tmsg.showinfo("入力されたテキスト",b)
#メインプログラム
root=tk.Tk()
root.geometry("400x150")
root.title("数当てゲーム")
#ラベル
labell=tk.Label(root,text="数入れてね",font=("Helvetica,28"))
labell.place(x=20,y=20)
#テキストボックス
editboxl=tk.Entry(width=4,font=("Helvetica,28"))
editboxl.place(x=120,y=60)
#ボタン
button1=tk.Button(root,text="チェック",font=("Helvetica"),command=ButtonClick)
#command はクリックされたとき実行する関数
button1.place(x=220,y=60)
#window表示
root.mainloop()
- getメソッドで入力欄の文字を表示
example06-05-02
# coding:utf-8
# 前に作成したゲームを入れる
import random
import tkinter as tk
import tkinter.messagebox as tmsg
# ボタンがクリックされたと時の処理
def ButtonClick():
b = editboxl.get()
isok = False
if len(b) != 4: # もしbが4桁ではないとき
tmsg.showerror("エラー", "4桁の数字を入力してください")
else:
kazuok = True
for i in range(4):
if (b[i] < "0") or (b[i] > "9"):
tmsg.showerror("エラー", "数字ではありません")
kazuok = False
break
if kazuok:
isok = True
if isok:
hit = 0
for i in range(4):
if a[i] == int(b[i]):
hit=hit+1
blow = 0
for j in range(4):
for i in range(4):
if (int(b[j]) == a[i]) and (int(b[i]) != a[i]) and (int(b[j]) != a[j]):
blow=blow+1
break
if hit == 4:
tmsg.showinfo("当たり", "おめでとうございます。当たりです")
# 終了
root.destroy()
else:
tmsg.showinfo("ヒント", "ヒット"+str(hit)+"/"+"ブロー"+str(blow))
# メインプログラム
a = [random.randint(0, 9),
random.randint(0, 9),
random.randint(0, 9),
random.randint(0, 9)]
root = tk.Tk()
root.geometry("400x150")
root.title("数当てゲーム")
# ラベル
labell = tk.Label(root, text="数入れてね", font=("Helvetica,28"))
labell.place(x=20, y=20)
# テキストボックス
editboxl = tk.Entry(width=4, font=("Helvetica,28"))
editboxl.place(x=120, y=60)
# ボタン
button1 = tk.Button(root, text="チェック", font=("Helvetica"), command=ButtonClick)
# command はクリックされたとき実行する関数
button1.place(x=220, y=60)
# window表示
root.mainloop()
- ゲームとウィンドウを融合
- ゲーム内での値をPrintではなくすべてメッセージボックスを用いて表す
履歴を表示
example06-06-01
#coding:utf-8
#前に作成したゲームを入れる
import random
import tkinter as tk
import tkinter.messagebox as tmsg
# ボタンがクリックされたと時の処理
def ButtonClick():
b = editboxl.get()
isok = False
if len(b) != 4: # もしbが4桁ではないとき
tmsg.showerror("エラー", "4桁の数字を入力してください")
else:
kazuok = True
for i in range(4):
if (b[i] < "0") or (b[i] > "9"):
tmsg.showerror("エラー", "数字ではありません")
kazuok = False
break
if kazuok:
isok = True
if isok:
hit = 0
for i in range(4):
if a[i] == int(b[i]):
hit=hit+1
blow = 0
for j in range(4):
for i in range(4):
if (int(b[j]) == a[i]) and (int(b[i]) != a[i]) and (int(b[j]) != a[j]):
blow=blow+1
break
if hit == 4:
tmsg.showinfo("当たり", "おめでとうございます。当たりです")
# 終了
root.destroy()
else:
#ヒット&ブローの表示
rirekibox.insert(tk.END,b+" /H:"+str(hit)+" B:"+str(blow)+"\n")
# メインプログラム
a = [random.randint(0, 9),
random.randint(0, 9),
random.randint(0, 9),
random.randint(0, 9)]
root = tk.Tk()
root.geometry("600x400")
root.title("数当てゲーム")
# 履歴表示のテキストボックスを作成
rirekibox=tk.Text(root,font=("Helvetica",14))
rirekibox.place(x=400,y=0,width=200,height=400)
# ラベル
labell = tk.Label(root, text="数入れてね", font=("Helvetica,28"))
labell.place(x=20, y=20)
# テキストボックス
editboxl = tk.Entry(width=4, font=("Helvetica,28"))
editboxl.place(x=120, y=60)
# ボタン
button1 = tk.Button(root, text="チェック", font=("Helvetica"), command=ButtonClick)
# command はクリックされたとき実行する関数
button1.place(x=220, y=60)
# window表示
root.mainloop()
- Textメソッドで別のテキストボックスをつくる
- width, heightを加える- 幅や高さを設定して見やすくする
- 大事なのは「はずれ」の時の履歴
- テキストボックスに文字追加ー insertメソッド
- tk.ENDで末尾に挿入する
ゲーム実行
- ctr+x, ctr+zなども便利
- ゲームと見た目を分けてつくる方が効率的かも。