반응형

첫번째..


예제보고 그냥 따라쳐보기

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from tkinter import *
 
        
 
window = Tk()
window.title("Calculator")
 
display = Entry(window, width=33, bg="yellow")
display.grid(row=0, column=0, columnspan=5)
 
buttonText = [
        '7''8''9''/' ,'C',
        '4''5''6''*'' ',
        '1''2''3''-'' ',
        '0''.''=''+''끝' ]
 
 
def click(key) :        
        if key == '=' :
                result = eval(display.get())
                s = str(result)
                display.insert(END, '='+s)
        elif key == '끝' :
                window.destroy()
        else :
                display.insert(END, key)
 
rowIndex = 1
colIndex = 0
 
for button_Text in buttonText :
        def process(t=button_Text) :
                click(t)
        Button(window, text=button_Text, width=5, command=process).grid(row=rowIndex, column=colIndex)
        colIndex += 1
        if colIndex>4 :
                rowIndex += 1
                colIndex = 0
 
 
       
window.mainloop()
 
cs


반응형
반응형
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import turtle
import random
 
turtle = turtle.Turtle()
turtle.shape("turtle")
turtle.speed(0)
 
= 0
= 0
 
#사각형 그리는 변수
rectX = x-20    #사각형 시작 x좌표값
rectY = y       #사각형 시작 y좌표값
rectWidth = 40  #사각형 가로 넓이
rectHeight = 80 #사각형 세로 길이
 
triSide = rectWidth + 40 #삼각형 한 변 길이
 
def drawRectangle(t, color , x, y, width, height) :
        t.fillcolor(color)
        t.penup()
        t.goto(x, y)
        t.pendown()
        t.begin_fill()
 
        for i in range(2) : 
                t.forward(width)
                t.right(90)
                t.forward(height)
                t.right(90)
        t.end_fill()
 
def drawTriangle(t, color, x, y, side) :
        t.fillcolor(color)
        t.penup()
        t.goto(x, y)
        t.pendown()
        t.begin_fill()
 
        t.forward(side)
        t.left(120)
        t.forward(side)
        t.left(120)
        t.forward(side)
 
        t.end_fill()
        
 
        
 
drawRectangle(turtle, "brown", rectX, rectY, rectWidth, rectHeight)
drawTriangle(turtle, "green", rectX-20, y, triSide)
 
cs


반응형
반응형

방향키 사용하여 랜덤으로 배치되는 동그라미까지 이동하기


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import random
import turtle
 
#거북이로 동그라미까지 이동하기
 
player = turtle.Turtle()  #사용할 거북이
player.color("blue")
player.shape("turtle")
player.speed(0)
player.penup()
player.goto(random.randint(-300,300), random.randint(-300300))
screen = player.getscreen()
 
a1 = turtle.Turtle() #목표지점1
a1.color("red")
a1.shape("circle")
a1.speed(0)
a1.penup()
a1.goto(random.randint(-300,300), random.randint(-300300))
 
a2 = turtle.Turtle() #목표지점1
a2.color("red")
a2.shape("circle")
a2.speed(0)
a2.penup()
a2.goto(random.randint(-300,300), random.randint(-300300))
 
def left() :
        player.left(30)
def right():
        player.right(30)
def up() :
        player.forward(30)
def down() :
        player.backward(30)
 
screen.onkeypress(left, "Left")
screen.onkeypress(right, "Right")
screen.onkeypress(up, "Up")
screen.onkeypress(down, "Down")
screen.listen()
 
 
def play() :
        playser.forward(2)
        a1.forward(2)
        a2(forward2)
        screen.ontimer(play, 10)
 
screen.ontimer(play, 10)
 
cs


반응형

+ Recent posts