2019年5月29日水曜日

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

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



#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()


メモ

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