LoginSignup

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

More than 3 years have passed since last update.

6章

Last updated at Posted at 2020-05-29

はじめのコード

# coding:utf-8

import random
import tkinter as tk
import tkinter.messagebox as tmsg
  • asで次の指示を省略できる

ボタンがクリックされた時の処理

def ButtonClick():

テキスト入力欄に入力された文字列を取得

b = editbox1.get()

Lesson 5-4 の部分から

今回のミス部分
# 4桁の数字かどうか判定する
    isok = False
    if len(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 :
  • 今回エラーになったelse書き直してもう一度Run(else)

4桁の数字であったとき

ヒット判定

    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 (a[i] != int(b[i])) and (a[j] != int(b[j])):
          blow = blow + 1
          break

 ヒットが4なら当たりで終了

    if hit == 4:
        tmsg.showinfo("当たり", "おめでとうございます。当たりです")
  • ==は比較演算子
  • tmsg.showinfo(タイトル,表示したい文字)

終了

        root.destroy()
    else:
  • destroyメゾットでゲーム終了

 ヒット数とブロー数を表示

            rirekibox.insert(tk.END, b + " /H:" + str(hit) + " B:" + str(blow) + "\n")
  • insertメゾット
    tk.ENDを指定して末尾に挿入

メインのプログラム

最初にランダムな4つの数字

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("数当てゲーム")
  • geometryメゾット
    横幅×高さ

履歴表示のテキストボックスを作る

rirekibox = tk.Text(root, font=("Helvetica", 14))
rirekibox.place(x=400, y=0, width=200, height=400)
  • Textメゾットを使用
    ボックスの場所、横幅、縦幅を指定

ラベルを作る

label1 = tk.Label(root, text="数を入力してね", font=("Helvetica", 14))
label1.place(x = 20, y = 20)
  • labelメゾット
    rootはテキストを張り付ける対象のウィンドウ・作成したラベルを変数へ代入・placeメゾットで位置を指定

テキストボックスを作る

editbox1 = tk.Entry(width = 4, font=("Helvetica", 28))
editbox1.place(x = 120, y = 60)
  • エントリーメゾットの利用
  • Entry
    テキスト入力欄を作る・4文字分の大きさ

ボタンを作る

button1 = tk.Button(root, text = "チェック", font=("Helvetica", 14), command=ButtonClick)
button1.place(x = 220, y = 60)
  • ボタンメゾット(text="ボタン名",フォント,サイズ)
  • command=でクリックされたときに関数を実行

  • font=(フォント名、フォントサイズ)

  • Helvetica:ゴシック体

  • Times:明朝体

  • Courier:等幅のタイプライタ

ウィンドウを表示する

root.mainloop()

6章!
Python

今回の間違え
1fontがfrontになっており、tkinterでエラーになった
2else:の書き始めのずれでエラーになった
感想:他にあまりミスはなかった。今回は5章の数当てゲームをtkinterを用いてゲームらしい見た目にした。また履歴を書き加える技も組み込まれていた。ずっと¥nの部分で教科書にはスラッシュと表記されているので迷っていたが、スラッシュと¥がイコールになることに注意したい。また、間違っていなくても赤い下線が表示されたときはその行をすべてもう一度書き直すとうまくいくことがある。
キャプチャ.PNG
知識:as       書き換え
geometry    ウィンドウのサイズ
place      x、y座標を使った位置づけ
root      変数
mainloop    ウィンドウの表示
label      メッセージの貼り付け
button      ボタンを作る
command=関数名 ButtonClickの関数を実行

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