2019年5月31日金曜日

クリックしたところに円と四角が描かれるプログラム その5

クリックしたところに円と四角が描かれるプログラム その5です。



#coding:utf-8

import tkinter as tk


def click(event):
    if editbox1.get() == '':
        pass
    else:
        set_size = int(editbox1.get())
        canvas.create_oval(event.x - set_size, event.y - set_size, event.x + set_size, event.y + set_size, fill = set_color, width = 0)

def click2(event):
    if editbox1.get() == '':
        pass
    else:
        set_size = int(editbox1.get())
        canvas.create_rectangle(event.x - set_size, event.y - set_size, event.x + set_size, event.y + set_size, fill = set_color, width = 0)

def reset():
    canvas.create_rectangle(0, 0, 600, 400, fill = 'white', width = 0)

def blue():
    global set_color
    set_color = 'blue'

def red():
    global set_color
    set_color = 'red'

def green():
    global set_color
    set_color = 'green'

def white():
    global set_color
    set_color = 'white'

def yellow():
    global set_color
    set_color = 'yellow'

def black():
    global set_color
    set_color = 'black'

root = tk.Tk()
root.geometry('900x400')
root.title('クリックすると円と四角が描かれる')

button1 = tk.Button(root,text = 'リセット', font = ('Courier',14),command = reset)
b_blue = tk.Button(root,text = ' 青 ', font = ('Courier',14),command = blue)
b_red = tk.Button(root,text = ' 赤 ', font = ('Courier',14),command = red)
b_green = tk.Button(root,text = ' 緑 ', font = ('Courier',14),command = green)
b_white = tk.Button(root,text = ' 白 ', font = ('Courier',14),command = white)
b_yellow = tk.Button(root,text = ' 黄 ', font = ('Courier',14),command = yellow)
b_black = tk.Button(root,text = ' 黒 ', font = ('Courier',14),command = black)
label1 = tk.Label(root,text = 'サイズを入力', font = ('Courier',14))
editbox1 = tk.Entry(width = 4, font = ('Courier',14))

canvas = tk.Canvas(root, width = 600, height = 400, bg = 'white')

button1.place(x = 700, y = 350)
b_blue.place(x = 620, y = 20)
b_red.place(x = 710, y = 20)
b_green.place(x = 800, y = 20)
b_white.place(x = 620, y = 70)
b_yellow.place(x = 710, y = 70)
b_black.place(x = 800, y = 70)
label1.place(x = 620, y = 120)
editbox1.place(x = 650, y = 150)

canvas.place(x = 0, y = 0)

set_color = 'red'

canvas.bind('<Button-1>', click)
canvas.bind('<Button-3>', click2)

root.mainloop()


メモ

・丸や四角のサイズを変更できるようにしました。
・サイズを入力していない間は、何も描かれないようにしました。

2019年5月30日木曜日

クリックしたところに円と四角が描かれるプログラム その4

クリックしたところに円と四角が描かれるプログラム その4です。



#coding:utf-8

import tkinter as tk


def click(event):
    canvas.create_oval(event.x - 20, event.y -20, event.x +20, event.y +20, fill = set_color, width = 0)

def click2(event):
    canvas.create_rectangle(event.x - 20, event.y -20, event.x +20, event.y +20, fill = set_color, width = 0)

def reset():
    canvas.create_rectangle(0, 0, 600, 400, fill = 'white', width = 0)

def blue():
    global set_color
    set_color = 'blue'

def red():
    global set_color
    set_color = 'red'

def green():
    global set_color
    set_color = 'green'

def white():
    global set_color
    set_color = 'white'

def yellow():
    global set_color
    set_color = 'yellow'

def black():
    global set_color
    set_color = 'black'

root = tk.Tk()
root.geometry('900x400')
root.title('クリックすると円と四角が描かれる')

button1 = tk.Button(root,text = 'リセット', font = ('Courier',14),command = reset)
b_blue = tk.Button(root,text = ' 青 ', font = ('Courier',14),command = blue)
b_red = tk.Button(root,text = ' 赤 ', font = ('Courier',14),command = red)
b_green = tk.Button(root,text = ' 緑 ', font = ('Courier',14),command = green)
b_white = tk.Button(root,text = ' 白 ', font = ('Courier',14),command = white)
b_yellow = tk.Button(root,text = ' 黄 ', font = ('Courier',14),command = yellow)
b_black = tk.Button(root,text = ' 黒 ', font = ('Courier',14),command = black)
canvas = tk.Canvas(root, width = 600, height = 400, bg = 'white')

button1.place(x = 700, y = 350)
b_blue.place(x = 620, y = 20)
b_red.place(x = 710, y = 20)
b_green.place(x = 800, y = 20)
b_white.place(x = 620, y = 70)
b_yellow.place(x = 710, y = 70)
b_black.place(x = 800, y = 70)
canvas.place(x = 0, y = 0)

set_color = 'red'

canvas.bind('<Button-1>', click)
canvas.bind('<Button-3>', click2)

root.mainloop()


メモ

・色を選択できるようにしました。

クリックしたところに円と四角が描かれるプログラム その3

クリックしたところに円と四角が描かれるプログラム その3です。



#coding:utf-8


import tkinter as tk

def click(event):
    canvas.create_oval(event.x - 20, event.y -20, event.x +20, event.y +20, fill = 'red', width = 0)

def click2(event):
    canvas.create_rectangle(event.x - 20, event.y -20, event.x +20, event.y +20, fill = 'blue', width = 0)

def reset():
    canvas.create_rectangle(0, 0, 600, 400, fill = 'white', width = 0)

root = tk.Tk()
root.geometry('900x400')
root.title('クリックすると円と四角が描かれる')

button1 = tk.Button(root,text = 'リセット', font = ('Courier',14),command = reset)
canvas = tk.Canvas(root, width = 600, height = 400, bg = 'white')

button1.place(x = 700, y = 350)
canvas.place(x = 0, y = 0)

canvas.bind('<Button-1>', click)
canvas.bind('<Button-3>', click2)

root.mainloop()


メモ

・リセットボダンを作りました。

2019年5月29日水曜日

クリックしたところに円と四角が描かれるプログラム その2

クリックしたところに円と四角が描かれるプログラム その2です。



#coding:utf-8


import tkinter as tk

def click(event):
    canvas.create_oval(event.x - 20, event.y -20, event.x +20, event.y +20, fill = 'red', width = 0)

def click2(event):
    canvas.create_rectangle(event.x - 20, event.y -20, event.x +20, event.y +20, fill = 'blue', width = 0)

def click3(event):
    canvas.create_rectangle(0, 0, 600, 400, fill = 'white', width = 0)

root = tk.Tk()
root.geometry('600x400')
root.title('クリックすると円と四角が描かれる')

canvas = tk.Canvas(root, width = 600, height = 400, bg = 'white')
canvas.place(x = 0, y = 0)

canvas.bind('<Button-1>', click)
canvas.bind('<Button-2>', click3)
canvas.bind('<Button-3>', click2)

root.mainloop()


メモ

・「いちばんやさしいPython入門教室」という本を参考にしています。
・マウスの中央ボタンを押すと白紙になるようにしました。

クリックしたところに円と四角が描かれるプログラム

クリックしたところに円と四角が描かれるプログラムです。



#coding:utf-8


import tkinter as tk

def click(event):
    canvas.create_oval(event.x - 20, event.y -20, event.x +20, event.y +20, fill = 'red', width = 0)

def click2(event):
    canvas.create_rectangle(event.x - 20, event.y -20, event.x +20, event.y +20, fill = 'blue', width = 0)

root = tk.Tk()
root.geometry('600x400')
root.title('クリックすると円と四角が描かれる')

canvas = tk.Canvas(root, width = 600, height = 400, bg = 'white')
canvas.place(x = 0, y = 0)

canvas.bind('<Button-1>', click)
canvas.bind('<Button-3>', click2)

root.mainloop()


メモ

・「いちばんやさしいPython入門教室」という本を参考にしています。
・右クリックすると四角が描かれるように追加しました。

クリックしたところに円が描かれるプログラム

クリックしたところに円が描かれるプログラムです。



#coding:utf-8


import tkinter as tk

def click(event):
    canvas.create_oval(event.x - 20, event.y -20, event.x +20, event.y +20, fill = 'red', width = 0)

root = tk.Tk()
root.geometry('600x400')
root.title('クリックすると円が描かれる')

canvas = tk.Canvas(root, width = 600, height = 400, bg = 'white')
canvas.place(x = 0, y = 0)

canvas.bind('<Button-1>', click)

root.mainloop()


メモ

・「いちばんやさしいPython入門教室」という本を参考にしています。

2019年5月28日火曜日

日本の国旗を描くプログラム

日本の国旗を描くプログラムです。




#coding:utf-8


import tkinter as tk

root = tk.Tk()
root.geometry('600x400')
root.title('日本の国旗を描く')



canvas = tk.Canvas(root,width = 600 ,height = 400 ,bg = '#66cdaa')
canvas.place(x = 0 ,y = 0)

canvas.create_rectangle(300 - 70 , 200 - 50 , 300 + 70 , 200 + 50 , width = 2 , fill = 'white')
canvas.create_oval(300 - 20 , 200 - 20 , 300 + 20 , 200 + 20 , fill = 'red' , width = 0)


root.mainloop()


メモ

・四角と丸で描けるのはこれくらいしか思いつかなかった。

円を描くプログラム その2

円を描くプログラム その2です。




#coding:utf-8


import tkinter as tk

root = tk.Tk()
root.geometry('600x400')
root.title('円を描く')



canvas = tk.Canvas(root,width = 600 ,height = 400 ,bg = '#66cdaa')
canvas.place(x = 0 ,y = 0)

canvas.create_oval(300 - 50 , 200 - 50 , 300 + 50 , 200 + 50 , width = 10)
canvas.create_oval(300 - 20 , 200 - 20 , 300 + 20 , 200 + 20 , outline = 'blue' , width = 3)
canvas.create_oval(300 - 15 , 200 - 15 , 300 + 15 , 200 + 15 , outline = '#ffffff')
canvas.create_oval(300 - 10 , 200 - 10 , 300 + 10 , 200 + 10 , fill = 'red')
canvas.create_oval(300 - 5 , 200 - 5 , 300 + 5 , 200 + 5 , fill = 'white')



root.mainloop()
 

メモ

・「いちばんやさしいPython入門教室」という本を参考にしています。
・色をつけたり・線の太さを変えたりしています。

2019年5月24日金曜日

円を描くプログラム

円を描くプログラムです。




#coding:utf-8


import tkinter as tk

root = tk.Tk()
root.geometry('600x400')
root.title('円を描く')



canvas = tk.Canvas(root,width = 600 ,height = 400 ,bg = '#66cdaa')
canvas.place(x = 0 ,y = 0)

canvas.create_oval(300 - 50 , 200 - 50 , 300 + 50 , 200 + 50)
canvas.create_oval(300 - 20 , 200 - 20 , 300 + 20 , 200 + 20)
canvas.create_oval(300 - 15 , 200 - 15 , 300 + 15 , 200 + 15)
canvas.create_oval(300 - 10 , 200 - 10 , 300 + 10 , 200 + 10)
canvas.create_oval(300 - 5 , 200 - 5 , 300 + 5 , 200 + 5)



root.mainloop()
 

メモ

・「いちばんやさしいPython入門教室」という本を参考にしています。

2019年5月22日水曜日

じゃんけんゲーム(ゲーム画面あり)のプログラム その9

じゃんけんゲーム(ゲーム画面あり)のプログラム その9です。




#coding:utf-8

import tkinter as tk
import random



def click0():
    if editbox1.get() == '':
        rirekibox.insert('1.0','名前を入力して下さい' + '\n')
    else:
        rirekibox.insert('1.0',print_hand(0,editbox1.get()) + '\n')
        computer_hand = random.randint(0,2)
        rirekibox.insert('2.0',print_hand(computer_hand,'コンピューター') + '\n')
        result = judge(0, computer_hand)
        rirekibox.insert('3.0','結果は' + result + 'でした' + '\n')
        rirekibox.insert('4.0','-'*30 + '\n')
def click1():
    if editbox1.get() == '':
        rirekibox.insert('1.0','名前を入力して下さい' + '\n')
    else:
        rirekibox.insert('1.0',print_hand(1,editbox1.get()) + '\n')
        computer_hand = random.randint(0,2)
        rirekibox.insert('2.0',print_hand(computer_hand,'コンピューター') + '\n')
        result = judge(1, computer_hand)
        rirekibox.insert('3.0','結果は' + result + 'でした' + '\n')
        rirekibox.insert('4.0','-'*30 + '\n')
def click2():
    if editbox1.get() == '':
        rirekibox.insert('1.0','名前を入力して下さい' + '\n')
    else:
        rirekibox.insert('1.0',print_hand(2,editbox1.get()) + '\n')
        computer_hand = random.randint(0,2)
        rirekibox.insert('2.0',print_hand(computer_hand,'コンピューター') + '\n')
        result = judge(2, computer_hand)
        rirekibox.insert('3.0','結果は' + result + 'でした' + '\n')
        rirekibox.insert('4.0','-'*30 + '\n')

def click3():
    rirekibox.delete('1.0','end')
    

def print_hand(hand, name):
    hands = ['グー', 'チョキ', 'パー']
    return name + 'は' + hands[hand] + 'を出しました'

def judge(player, computer):
    if player == computer:
        return '引き分け'
    elif player == 0 and computer == 1:
        return '勝ち'
    elif player == 1 and computer == 2:
        return '勝ち'
    elif player == 2 and computer == 0:
        return '勝ち'
    else:
        return '負け'
    

root = tk.Tk()
root.geometry('800x380')
root.title('じゃんけんゲーム')

label1 = tk.Label(root,text = 'じゃんけんをはじめます。', font = ('Courier',14))
label2 = tk.Label(root,text = '名前を入力して下さい。', font = ('Courier',14))
editbox1 = tk.Entry(width = 15, font = ('Courier',14))
label3 =tk.Label(root,text = '何を出しますか?', font = ('Courier',14))
button0 = tk.Button(root,text = ' グー  ', font = ('Courier',14),command = click0)
button1 = tk.Button(root,text = ' チョキ ', font = ('Courier',14),command = click1)
button2 = tk.Button(root,text = ' パー  ', font = ('Courier',14),command = click2)
button3 = tk.Button(root,text = '結果削除', font = ('Courier',14),command = click3)
rirekibox = tk.Text(root, font = ('Courier',10))

label1.place(x = 20, y = 20)
label2.place(x = 20, y = 50)
editbox1.place(x = 40, y = 90)
label3.place(x = 20, y = 150)
button0.place(x = 20, y = 190)
button1.place(x = 150, y = 190)
button2.place(x = 280, y = 190)
button3.place(x = 320, y = 300)
rirekibox.place(x = 500, y =0, width = 300, height = 380)




root.mainloop()


メモ

・「---------------」の表示を「'-'*30」のように書き換えました。繰り返しの時はこのほうがスッキリするようですね。

じゃんけんゲーム(ゲーム画面あり)のプログラム その8

じゃんけんゲーム(ゲーム画面あり)のプログラム その8です。




#coding:utf-8

import tkinter as tk
import random


def click0():
    if editbox1.get() == '':
        rirekibox.insert('1.0','名前を入力して下さい' + '\n')
    else:
        rirekibox.insert('1.0',print_hand(0,editbox1.get()) + '\n')
        computer_hand = random.randint(0,2)
        rirekibox.insert('2.0',print_hand(computer_hand,'コンピューター') + '\n')
        result = judge(0, computer_hand)
        rirekibox.insert('3.0','結果は' + result + 'でした' + '\n')
        rirekibox.insert('4.0','---------------------------' + '\n')
def click1():
    if editbox1.get() == '':
        rirekibox.insert('1.0','名前を入力して下さい' + '\n')
    else:
        rirekibox.insert('1.0',print_hand(1,editbox1.get()) + '\n')
        computer_hand = random.randint(0,2)
        rirekibox.insert('2.0',print_hand(computer_hand,'コンピューター') + '\n')
        result = judge(1, computer_hand)
        rirekibox.insert('3.0','結果は' + result + 'でした' + '\n')
        rirekibox.insert('4.0','---------------------------' + '\n')
def click2():
    if editbox1.get() == '':
        rirekibox.insert('1.0','名前を入力して下さい' + '\n')
    else:
        rirekibox.insert('1.0',print_hand(2,editbox1.get()) + '\n')
        computer_hand = random.randint(0,2)
        rirekibox.insert('2.0',print_hand(computer_hand,'コンピューター') + '\n')
        result = judge(2, computer_hand)
        rirekibox.insert('3.0','結果は' + result + 'でした' + '\n')
        rirekibox.insert('4.0','---------------------------' + '\n')

def click3():
    rirekibox.delete('1.0','end')
    

def print_hand(hand, name):
    hands = ['グー', 'チョキ', 'パー']
    return name + 'は' + hands[hand] + 'を出しました'

def judge(player, computer):
    if player == computer:
        return '引き分け'
    elif player == 0 and computer == 1:
        return '勝ち'
    elif player == 1 and computer == 2:
        return '勝ち'
    elif player == 2 and computer == 0:
        return '勝ち'
    else:
        return '負け'
    

root = tk.Tk()
root.geometry('800x380')
root.title('じゃんけんゲーム')

label1 = tk.Label(root,text = 'じゃんけんをはじめます。', font = ('Courier',14))
label2 = tk.Label(root,text = '名前を入力して下さい。', font = ('Courier',14))
editbox1 = tk.Entry(width = 15, font = ('Courier',14))
label3 =tk.Label(root,text = '何を出しますか?', font = ('Courier',14))
button0 = tk.Button(root,text = ' グー  ', font = ('Courier',14),command = click0)
button1 = tk.Button(root,text = ' チョキ ', font = ('Courier',14),command = click1)
button2 = tk.Button(root,text = ' パー  ', font = ('Courier',14),command = click2)
button3 = tk.Button(root,text = '結果削除', font = ('Courier',14),command = click3)
rirekibox = tk.Text(root, font = ('Courier',10))

label1.place(x = 20, y = 20)
label2.place(x = 20, y = 50)
editbox1.place(x = 40, y = 90)
label3.place(x = 20, y = 150)
button0.place(x = 20, y = 190)
button1.place(x = 150, y = 190)
button2.place(x = 280, y = 190)
button3.place(x = 320, y = 300)
rirekibox.place(x = 500, y =0, width = 300, height = 380)




root.mainloop()


メモ

・右側の結果表示の仕方を変えました。下に追加していくと見えなくなるので上に追加されるように変更。

2019年5月21日火曜日

じゃんけんゲームのゲーム画面のプログラム その7

じゃんけんゲームのゲーム画面のプログラム その7です。




#coding:utf-8

import tkinter as tk
import random


def click0():
    if editbox1.get() == '':
        rirekibox.insert(tk.END,'名前を入力して下さい' + '\n')
    else:
        rirekibox.insert(tk.END,print_hand(0,editbox1.get()) + '\n')
        computer_hand = random.randint(0,2)
        rirekibox.insert(tk.END,print_hand(computer_hand,'コンピューター') + '\n')
        result = judge(0, computer_hand)
        rirekibox.insert(tk.END,'結果は' + result + 'でした' + '\n')
        rirekibox.insert(tk.END,'---------------------------' + '\n')
def click1():
    if editbox1.get() == '':
        rirekibox.insert(tk.END,'名前を入力して下さい' + '\n')
    else:
        rirekibox.insert(tk.END,print_hand(1,editbox1.get()) + '\n')
        computer_hand = random.randint(0,2)
        rirekibox.insert(tk.END,print_hand(computer_hand,'コンピューター') + '\n')
        result = judge(1, computer_hand)
        rirekibox.insert(tk.END,'結果は' + result + 'でした' + '\n')
        rirekibox.insert(tk.END,'---------------------------' + '\n')
def click2():
    if editbox1.get() == '':
        rirekibox.insert(tk.END,'名前を入力して下さい' + '\n')
    else:
        rirekibox.insert(tk.END,print_hand(2,editbox1.get()) + '\n')
        computer_hand = random.randint(0,2)
        rirekibox.insert(tk.END,print_hand(computer_hand,'コンピューター') + '\n')
        result = judge(2, computer_hand)
        rirekibox.insert(tk.END,'結果は' + result + 'でした' + '\n')
        rirekibox.insert(tk.END,'---------------------------' + '\n')

def click3():
    rirekibox.delete('1.0','end')
    

def print_hand(hand, name):
    hands = ['グー', 'チョキ', 'パー']
    return name + 'は' + hands[hand] + 'を出しました'

def judge(player, computer):
    if player == computer:
        return '引き分け'
    elif player == 0 and computer == 1:
        return '勝ち'
    elif player == 1 and computer == 2:
        return '勝ち'
    elif player == 2 and computer == 0:
        return '勝ち'
    else:
        return '負け'
    

root = tk.Tk()
root.geometry('800x380')
root.title('じゃんけんゲーム')

label1 = tk.Label(root,text = 'じゃんけんをはじめます。', font = ('Courier',14))
label2 = tk.Label(root,text = '名前を入力して下さい。', font = ('Courier',14))
editbox1 = tk.Entry(width = 15, font = ('Courier',14))
label3 =tk.Label(root,text = '何を出しますか?', font = ('Courier',14))
button0 = tk.Button(root,text = ' グー  ', font = ('Courier',14),command = click0)
button1 = tk.Button(root,text = ' チョキ ', font = ('Courier',14),command = click1)
button2 = tk.Button(root,text = ' パー  ', font = ('Courier',14),command = click2)
button3 = tk.Button(root,text = '結果削除', font = ('Courier',14),command = click3)
rirekibox = tk.Text(root, font = ('Courier',10))

label1.place(x = 20, y = 20)
label2.place(x = 20, y = 50)
editbox1.place(x = 40, y = 90)
label3.place(x = 20, y = 150)
button0.place(x = 20, y = 190)
button1.place(x = 150, y = 190)
button2.place(x = 280, y = 190)
button3.place(x = 320, y = 300)
rirekibox.place(x = 500, y =0, width = 300, height = 380)




root.mainloop()


メモ

・ゲームが出来るようになりました。
・「結果削除」ボタンに変更して、右側に表示していた履歴を消すようにしました。

2019年5月20日月曜日

じゃんけんゲームのゲーム画面のプログラム その6

1から100までの数字を2の倍数、3の倍数、6の倍数かを調べるプログラムです。



#coding:utf-8

import tkinter as tk
import random


def click0():
    if editbox1.get() == '':
        rirekibox.insert(tk.END,'名前を入力して下さい' + '\n')
    else:
        rirekibox.insert(tk.END,print_hand(0,editbox1.get()) + '\n')
        computer_hand = random.randint(0,2)
        rirekibox.insert(tk.END,print_hand(computer_hand,'コンピューター') + '\n')
        result = judge(0, computer_hand)
        rirekibox.insert(tk.END,'結果は' + result + 'でした' + '\n')
        rirekibox.insert(tk.END,'---------------------------' + '\n')
def click1():
    if editbox1.get() == '':
        rirekibox.insert(tk.END,'名前を入力して下さい' + '\n')
    else:
        rirekibox.insert(tk.END,print_hand(1,editbox1.get()) + '\n')
        computer_hand = random.randint(0,2)
        rirekibox.insert(tk.END,print_hand(computer_hand,'コンピューター') + '\n')
        result = judge(1, computer_hand)
        rirekibox.insert(tk.END,'結果は' + result + 'でした' + '\n')
        rirekibox.insert(tk.END,'---------------------------' + '\n')
def click2():
    if editbox1.get() == '':
        rirekibox.insert(tk.END,'名前を入力して下さい' + '\n')
    else:
        rirekibox.insert(tk.END,print_hand(2,editbox1.get()) + '\n')
        computer_hand = random.randint(0,2)
        rirekibox.insert(tk.END,print_hand(computer_hand,'コンピューター') + '\n')
        result = judge(2, computer_hand)
        rirekibox.insert(tk.END,'結果は' + result + 'でした' + '\n')
        rirekibox.insert(tk.END,'---------------------------' + '\n')


def print_hand(hand, name):
    hands = ['グー', 'チョキ', 'パー']
    return name + 'は' + hands[hand] + 'を出しました'

def judge(player, computer):
    if player == computer:
        return '引き分け'
    elif player == 0 and computer == 1:
        return '勝ち'
    elif player == 1 and computer == 2:
        return '勝ち'
    elif player == 2 and computer == 0:
        return '勝ち'
    else:
        return '負け'
    

root = tk.Tk()
root.geometry('800x380')
root.title('じゃんけんゲーム')

label1 = tk.Label(root,text = 'じゃんけんをはじめます。', font = ('Courier',14))
label2 = tk.Label(root,text = '名前を入力して下さい。', font = ('Courier',14))
editbox1 = tk.Entry(width = 15, font = ('Courier',14))
label3 =tk.Label(root,text = '何を出しますか?', font = ('Courier',14))
button0 = tk.Button(root,text = ' グー  ', font = ('Courier',14),command = click0)
button1 = tk.Button(root,text = ' チョキ ', font = ('Courier',14),command = click1)
button2 = tk.Button(root,text = ' パー  ', font = ('Courier',14),command = click2)
button3 = tk.Button(root,text = 'ゲーム終了', font = ('Courier',14))
rirekibox = tk.Text(root, font = ('Courier',10))

label1.place(x = 20, y = 20)
label2.place(x = 20, y = 50)
editbox1.place(x = 40, y = 90)
label3.place(x = 20, y = 150)
button0.place(x = 20, y = 190)
button1.place(x = 150, y = 190)
button2.place(x = 280, y = 190)
button3.place(x = 320, y = 300)
rirekibox.place(x = 500, y =0, width = 300, height = 380)




root.mainloop()
 

メモ

・ゲームが出来るようになりました。
・コンピューターの手と結果が表示されるようになりました。

じゃんけんゲームのゲーム画面のプログラム その5

じゃんけんゲームのゲーム画面のプログラム その5です。




#coding:utf-8

import tkinter as tk

def click1():
    if editbox1.get() == '':
        rirekibox.insert(tk.END,'名前を入力して下さい' + '\n')
    else:
        rirekibox.insert(tk.END,print_hand(0,editbox1.get()) + '\n')
def click2():
    if editbox1.get() == '':
        rirekibox.insert(tk.END,'名前を入力して下さい' + '\n')
    else:
        rirekibox.insert(tk.END,print_hand(1,editbox1.get()) + '\n')
def click3():
    if editbox1.get() == '':
        rirekibox.insert(tk.END,'名前を入力して下さい' + '\n')
    else:
        rirekibox.insert(tk.END,print_hand(2,editbox1.get()) + '\n')


def print_hand(hand, name):
    hands = ['グー', 'チョキ', 'パー']
    return name + 'は' + hands[hand] + 'を出しました'


root = tk.Tk()
root.geometry('800x380')
root.title('じゃんけんゲーム')

label1 = tk.Label(root,text = 'じゃんけんをはじめます。', font = ('Courier',14))
label2 = tk.Label(root,text = '名前を入力して下さい。', font = ('Courier',14))
editbox1 = tk.Entry(width = 15, font = ('Courier',14))
label3 =tk.Label(root,text = '何を出しますか?', font = ('Courier',14))
button1 = tk.Button(root,text = ' グー  ', font = ('Courier',14),command = click1)
button2 = tk.Button(root,text = ' チョキ ', font = ('Courier',14),command = click2)
button3 = tk.Button(root,text = ' パー  ', font = ('Courier',14),command = click3)
button4 = tk.Button(root,text = 'ゲーム終了', font = ('Courier',14))
rirekibox = tk.Text(root, font = ('Courier',14))

label1.place(x = 20, y = 20)
label2.place(x = 20, y = 50)
editbox1.place(x = 40, y = 90)
label3.place(x = 20, y = 150)
button1.place(x = 20, y = 190)
button2.place(x = 150, y = 190)
button3.place(x = 280, y = 190)
button4.place(x = 320, y = 300)
rirekibox.place(x = 500, y =0, width = 300, height = 380)




root.mainloop()


メモ
・まだゲームは出来ません。
・画面表示をこんな感じにしたいなと思っています。
・「いちばんやさしいPython入門教室」という本を参考にしています。
・前回と表示はほとんど変わらないが、プレイヤーが何を出したのかを関数で処理するようにしています。

2019年5月18日土曜日

じゃんけんゲームのゲーム画面のプログラム その4

じゃんけんゲームのゲーム画面のプログラム その4です。




#coding:utf-8

import tkinter as tk

def click1():
    if editbox1.get() == '':
        rirekibox.insert(tk.END,'名前を入力して下さい' + '\n')
    else:
        rirekibox.insert(tk.END,editbox1.get() + 'は、グーを出しました' + '\n')
def click2():
    if editbox1.get() == '':
        rirekibox.insert(tk.END,'名前を入力して下さい' + '\n')
    else:    
        rirekibox.insert(tk.END,editbox1.get() + 'は、チョキを出しました'+'\n')
def click3():
    if editbox1.get() == '':
        rirekibox.insert(tk.END,'名前を入力して下さい' + '\n')
    else:
        rirekibox.insert(tk.END,editbox1.get() + 'は、パーを出しました'+'\n')


        



root = tk.Tk()
root.geometry('800x380')
root.title('じゃんけんゲーム')

label1 = tk.Label(root,text = 'じゃんけんをはじめます。', font = ('Courier',14))
label2 = tk.Label(root,text = '名前を入力して下さい。', font = ('Courier',14))
editbox1 = tk.Entry(width = 15, font = ('Courier',14))
label3 =tk.Label(root,text = '何を出しますか?', font = ('Courier',14))
button1 = tk.Button(root,text = ' グー  ', font = ('Courier',14),command = click1)
button2 = tk.Button(root,text = ' チョキ ', font = ('Courier',14),command = click2)
button3 = tk.Button(root,text = ' パー  ', font = ('Courier',14),command = click3)
button4 = tk.Button(root,text = 'ゲーム終了', font = ('Courier',14))
rirekibox = tk.Text(root, font = ('Courier',14))

label1.place(x = 20, y = 20)
label2.place(x = 20, y = 50)
editbox1.place(x = 40, y = 90)
label3.place(x = 20, y = 150)
button1.place(x = 20, y = 190)
button2.place(x = 150, y = 190)
button3.place(x = 280, y = 190)
button4.place(x = 320, y = 300)
rirekibox.place(x = 500, y =0, width = 300, height = 380)




root.mainloop()


メモ

・まだゲームは出来ません。
・画面表示をこんな感じにしたいなと思っています。
・「いちばんやさしいPython入門教室」という本を参考にしています。
・プレイヤーの名前を入力していない場合は「名前を入力して下さい」と表示されるようにした。

じゃんけんゲームのゲーム画面のプログラム その3

じゃんけんゲームのゲーム画面のプログラム その3です。




#coding:utf-8

import tkinter as tk

def click1():
    rirekibox.insert(tk.END,editbox1.get() + 'は、グーを出しました' + '\n')
def click2():
    rirekibox.insert(tk.END,editbox1.get() + 'は、チョキを出しました'+'\n')
def click3():
    rirekibox.insert(tk.END,editbox1.get() + 'は、パーを出しました'+'\n')


root = tk.Tk()
root.geometry('800x380')
root.title('じゃんけんゲーム')

label1 = tk.Label(root,text = 'じゃんけんをはじめます。', font = ('Courier',14))
label2 = tk.Label(root,text = '名前を入力して下さい。', font = ('Courier',14))
editbox1 = tk.Entry(width = 15, font = ('Courier',14))
label3 =tk.Label(root,text = '何を出しますか?', font = ('Courier',14))
button1 = tk.Button(root,text = ' グー  ', font = ('Courier',14),command = click1)
button2 = tk.Button(root,text = ' チョキ ', font = ('Courier',14),command = click2)
button3 = tk.Button(root,text = ' パー  ', font = ('Courier',14),command = click3)
button4 = tk.Button(root,text = 'ゲーム終了', font = ('Courier',14))
rirekibox = tk.Text(root, font = ('Courier',14))

label1.place(x = 20, y = 20)
label2.place(x = 20, y = 50)
editbox1.place(x = 40, y = 90)
label3.place(x = 20, y = 150)
button1.place(x = 20, y = 190)
button2.place(x = 150, y = 190)
button3.place(x = 280, y = 190)
button4.place(x = 320, y = 300)
rirekibox.place(x = 500, y =0, width = 300, height = 380)




root.mainloop()
  

メモ

・まだゲームは出来ません。
・画面表示をこんな感じにしたいなと思っています。
・「いちばんやさしいPython入門教室」という本を参考にしています。
・プレイヤーの名前を入力してそれが表示されるようにしました。

2019年5月17日金曜日

じゃんけんゲームのゲーム画面のプログラム その2

じゃんけんゲームのゲーム画面のプログラム その2です。



#coding:utf-8

import tkinter as tk

def click1():
    rirekibox.insert(tk.END,'グーを出しました'+'\n')
def click2():
    rirekibox.insert(tk.END,'チョキを出しました'+'\n')
def click3():
    rirekibox.insert(tk.END,'パーを出しました'+'\n')
    

root = tk.Tk()
root.geometry('800x380')
root.title('じゃんけんゲーム')

label1 = tk.Label(root,text = 'じゃんけんをはじめます。', font = ('Courier',14))
label2 = tk.Label(root,text = '名前を入力して下さい。', font = ('Courier',14))
editbox1 = tk.Entry(width = 15, font = ('Courier',14))
label3 =tk.Label(root,text = '何を出しますか?', font = ('Courier',14))
button1 = tk.Button(root,text = ' グー  ', font = ('Courier',14),command = click1)
button2 = tk.Button(root,text = ' チョキ ', font = ('Courier',14),command = click2)
button3 = tk.Button(root,text = ' パー  ', font = ('Courier',14),command = click3)
button4 = tk.Button(root,text = 'ゲーム終了', font = ('Courier',14))
rirekibox = tk.Text(root, font = ('Courier',14))

label1.place(x = 20, y = 20)
label2.place(x = 20, y = 50)
editbox1.place(x = 40, y = 90)
label3.place(x = 20, y = 150)
button1.place(x = 20, y = 190)
button2.place(x = 150, y = 190)
button3.place(x = 280, y = 190)
button4.place(x = 320, y = 300)
rirekibox.place(x = 500, y =0, width = 300, height = 380)




root.mainloop()


メモ

・まだゲームは出来ません。
・画面表示をこんな感じにしたいなと思っています。
・「いちばんやさしいPython入門教室」という本を参考にしています。
・右側に履歴表示されるようにしました。
・ボタンを押すと文字が表示されるようにしました。

2019年5月16日木曜日

じゃんけんゲームのゲーム画面のプログラム

じゃんけんゲームのゲーム画面のプログラムです。



#coding:utf-8

import tkinter as tk

root = tk.Tk()
root.geometry('500x380')
root.title('じゃんけんゲーム')

label1 = tk.Label(root,text = 'じゃんけんをはじめます。', font = ('Courier',14))
label2 = tk.Label(root,text = '名前を入力して下さい。', font = ('Courier',14))
editbox1 = tk.Entry(width = 15, font = ('Courier',14))
label3 =tk.Label(root,text = '何を出しますか?', font = ('Courier',14))
button1 = tk.Button(root,text = ' グー  ', font = ('Courier',14))
button2 = tk.Button(root,text = ' チョキ ', font = ('Courier',14))
button3 = tk.Button(root,text = ' パー  ', font = ('Courier',14))
button4 = tk.Button(root,text = 'ゲーム終了', font = ('Courier',14))

label1.place(x = 20, y = 20)
label2.place(x = 20, y = 50)
editbox1.place(x = 40, y = 90)
label3.place(x = 20, y = 150)
button1.place(x = 20, y = 190)
button2.place(x = 150, y = 190)
button3.place(x = 280, y = 190)
button4.place(x = 320, y = 300)

root.mainloop()


メモ

・まだゲームは出来ません。
・画面表示をこんな感じにしたいなと思っています。
・「いちばんやさしいPython入門教室」という本を参考にしています。

2019年5月13日月曜日

文字数をチェックするプログラム

文字数をチェックするプログラムです。

4文字を入力して下さい:123
4文字ではありません。やり直して下さい
4文字を入力して下さい:te3
4文字ではありません。やり直して下さい
4文字を入力して下さい:ab12
入力したのはab12で4文字です
>>> 


#coding:utf-8

a = False
while a == False:
    b = input('4文字を入力して下さい:')
    if len(b) != 4:
        print('4文字ではありません。やり直して下さい')
    else:
        a = True
print('入力したのは'+ b + 'で4文字です')  

メモ

・「len」は文字数やリストの数をカウント出来ます。

2019年5月9日木曜日

「Progate」のじゃんけんゲーム 改良その6

プログラムの学習サイト「Progate」の「Python学習コースⅢ」のじゃんけんゲームを改良したプログラム その6。

じゃんけんをはじめます
名前を入力してください:テストプレイヤー
--------------------------------------------------------------

何を出しますか?(0: ゲーム終了, 1: グー, 2: チョキ, 3: パー)
数字で入力してください:2

テストプレイヤーは 「チョキ」 を出しました
コンピューターは 「パー」 を出しました

結果は 「勝ち」 でした

1勝 0負 0引き分け です

--------------------------------------------------------------

何を出しますか?(0: ゲーム終了, 1: グー, 2: チョキ, 3: パー)
数字で入力してください:3

テストプレイヤーは 「パー」 を出しました
コンピューターは 「チョキ」 を出しました

結果は 「負け」 でした

1勝 1負 0引き分け です

--------------------------------------------------------------

何を出しますか?(0: ゲーム終了, 1: グー, 2: チョキ, 3: パー)
数字で入力してください:1

テストプレイヤーは 「グー」 を出しました
コンピューターは 「パー」 を出しました

結果は 「負け」 でした

1勝 2負 0引き分け です

--------------------------------------------------------------

何を出しますか?(0: ゲーム終了, 1: グー, 2: チョキ, 3: パー)
数字で入力してください:0

ゲームを終了します


#coding:utf-8

def validate(hand):
    if hand == '1' or hand == '2' or hand == '3' :
        return True
    else:
        return False

def print_hand(hand, name='ゲスト'):
    hands = ['グー', 'チョキ', 'パー']
    print(name + 'は 「' + hands[hand] + '」 を出しました')

def judge(player, computer):
    if player == computer:
        return '引き分け'
    elif player == 0 and computer == 1:
        return '勝ち'
    elif player == 1 and computer == 2:
        return '勝ち'
    elif player == 2 and computer == 0:
        return '勝ち'
    else:
        return '負け'


import random

player_win = 0
computer_win = 0
draw = 0
            
print('じゃんけんをはじめます')

player_name = input('名前を入力してください:')

while True :
    
    print('--------------------------------------------------------------')
    print('')
    print('何を出しますか?(0: ゲーム終了, 1: グー, 2: チョキ, 3: パー)')

    player_hand = input('数字で入力してください:')

    print('')

    if player_hand == '0':
        print('ゲームを終了します')
        break

    else:    
        if validate(player_hand):
            
            computer_hand = random.choice((0,1,2))

            
            
            if player_name == '':
                print_hand(int(player_hand)-1)
            else:
                print_hand(int(player_hand)-1, player_name)

            print_hand(computer_hand, 'コンピューター')
            
            result = judge(int(player_hand)-1, computer_hand)

            print('')
            print('結果は 「' + result + '」 でした')
        
            if result == '勝ち':
                player_win += 1
            elif result == '負け':
                computer_win += 1
            else :
                draw += 1
            
            print('')
            print(str(player_win) + '勝 ' + str(computer_win) + '負 ' + str(draw) + '引き分け です')
            print('')
            
        else:
            print('正しい数値を入力してください')


メモ

・ランダムで数字を作る部分を「choice」をつかったバージョンにした。

2019年5月8日水曜日

任意の数から数までの和を求めるプログラム その2。

任意の数から数までの和を求めるプログラム その2。

始まりの数字を入力して下さい:2
終わりの数字を入力して下さい:10
2 合計: 2
3 合計: 5
4 合計: 9
5 合計: 14
6 合計: 20
7 合計: 27
8 合計: 35
9 合計: 44
10 合計: 54


#coding:utf-8

def total(e,f):
   b = e
   for a in range(e,f+1):
      print(str(a) + ' 合計: ' + str(b))
      b += (a+1)

c = int(input('始まりの数字を入力して下さい:'))
d = int(input('終わりの数字を入力して下さい:'))

total(c,d)

メモ

・以前のプログラムを無駄に関数を使って作り直してみました。

2019年5月4日土曜日

任意の数から数までの和を求めるプログラム

任意の数から数までの和を求めるプログラム。

始まりの数字を入力して下さい:2
終わりの数字を入力して下さい:5
2 合計: 2
3 合計: 5
4 合計: 9
5 合計: 14


#coding:utf-8

c = int(input('始まりの数字を入力して下さい:'))
d = int(input('終わりの数字を入力して下さい:'))

b = c

for a in range(c,d+1):
   print(str(a) + ' 合計: ' + str(b))
   b += (a+1)

メモ

・変数が増えるとややこしくなるね。

2019年5月3日金曜日

任意の数字までの合計を出すプログラム

任意の数字までの合計を出すプログラム

終わりの数字を入力して下さい:12
1 合計: 1
2 合計: 3
3 合計: 6
4 合計: 10
5 合計: 15
6 合計: 21
7 合計: 28
8 合計: 36
9 合計: 45
10 合計: 55
11 合計: 66
12 合計: 78
>>> 


#coding:utf-8

b=0

c = int(input('終わりの数字を入力して下さい:'))

for a in range(c):
   b += (a+1)
   print(str(a+1) + ' 合計: ' + str(b))

メモ

・好きな数字までの和を出すように変更

2019年5月2日木曜日

数字の1から5までの数字を足すプログラム

数字の1から5までの数字を足すプログラムです。

1
3
6
10
15
合計:15


#coding:utf-8

b=0
for a in range(5):
   b += (a+1)
   print(b)
print('合計:' + str(b))

メモ

・「range(開始する値(以上になる),終了する値(未満になる))」で連続した数を扱うことができる。
・開始する値が0の場合は省略が可能。
・「for文」と混ぜると繰り返す回数を指定できる。
・終了する値が未満なのに注意