본문 바로가기
개발일지/Python

2022.02.13 파이썬 정복 18장 그래픽

by 개발에정착하고싶다 2022. 2. 13.
320x100
# 18장 그래픽

# 18.1 Thinker
'''
# 윈도우 생성
from tkinter import *
main = Tk()

main.mainloop()
'''

'''
# 위젯 배치
from tkinter import *
main = Tk()
main.title("Tk Test")
main.geometry("300x200")

lbl = Label(main, text="Label", font = "Arial 20")
lbl.pack()
apple = Button(main, text="Apple", foreground="Red")
apple.pack()
orange = Button(main, text="Orange", foreground="Green")
orange.pack()

main.mainloop()
'''

'''
main.resizable(False,False)

lbl = Label(main)
lbl = ["text"] = "Label"
lbl["font"]="Arial 20"

lbl.config(text="Label", font="Arial 20")
'''

'''
from tkinter import *
main = Tk()
main.title("Tk Test")
main.geometry("300x200")

lbl = Label(main, text="Label", font="Arial 20")
lbl.pack()

def applelick():
    lbl["text"] = "Apple"

apple = Button(main, text="Apple", foreground="Red", command=applelick)
apple.pack()

def orangeclick():
    lbl["text"] = "Orange"
orange = Button(main, text="Orange", foreground="Green", command=orangeclick)
orange.pack()

main.mainloop()
'''

'''
# 대화상자

from tkinter import *
import tkinter.messagebox

main =Tk()

def btnclick():
    if tkinter.messagebox.askyesno("질문","당신은 미성년자 입니까?"):
        tkinter.messagebox.showwarning("경고","애들은 가라")

    else:
        tkinter.messagebox.showinfo("환영","어서오세요 고객님")
btn = Button(main,text="입장",foreground="Blue", command=btnclick)
btn.pack()

main.mainloop()
'''

'''
from tkinter import *
import tkinter.messagebox
import tkinter.simpledialog

main = Tk()

def btnclick():
    name = tkinter.simpledialog.askstring("질문","이름을 입력하세요")
    age = tkinter.simpledialog.askinteger("질문","나이를 입력하세요")
    if name and age:
        tkinter.messagebox.showwarning("환영",str(age)+"세"+name+"님 반갑습니다.")

btn = Button(main, text="클릭", foreground="Blue", command= btnclick)
btn.pack()

main.mainloop()
'''

'''
from tkinter import*
import tkinter.messagebox
import tkinter.simpledialog

main = Tk()


def btnclick():
    name = tkinter.simpledialog.askstring("질문","이름을 입력하세요")
    age = tkinter.simpledialog.askinteger("질문","나이를 입력하세요")
    if name and age:
        tkinter.messagebox.showwarning("환영",str(age)+"세"+name+"님 반갑습니다.")

btn = Button(main, text ="클릭", foreground="Blue", command=btnclick)
btn.pack()

main.mainloop()
'''


'''
# 메뉴

from tkinter import *
import tkinter.messagebox

main = Tk()
main.title("Tk Test")
main.geometry("300x200")

menubar = Menu(main)
main.config(menu = menubar)
popup = Menu(menubar)
menubar.add_cascade(label = "파일",menu = popup)

def about():
    tkinter.messagebox.showinfo("소개","메뉴 사용 예제입니다.")

popup.add_command(label = "소개",command=about)
popup.add_command(label = "종료",command=quit)

main.mainloop()
'''

# 그래픽 출력

# 파이썬에서 Tk의 가치는 그래픽을 그려볼 수 있다는 것이라고 한다.

'''
from tkinter import *
main = Tk()

c = Canvas(main,width=400, height=200)
c.pack()

c.create_line(10,10,100,100)
c.create_line(10,100,100,10, fill="blue")
c.create_rectangle(110,10,200,100, outline="red",width=5)
c.create_oval(210,10,300,100,width=3, fill="yellow")

main.mainloop()
'''

'''
from tkinter import *
main = Tk()
c = Canvas(main,width = 500, height = 400)
c.pack()

img = PhotoImage(file = "child.gif")
c.create_image(10,10,image = img, anchor = NW)

main.mainloop()

# 이번예제는 어딘가에 보관되어있는 사진을 가져와서 출력하는것 같다.
# 하지만 역시는 역시인가 거지같은 저자는 이에대해 설명이 없다 ㅇㅇ
'''

# 18.2 터틀 그래픽

# 터틀 그래픽

'''
import turtle as t
t.shape("turtle")

t.forward(100)
t.right(90)
t.forward(100)
t.done()

# 여기부터는 예전에 유데미 파이썬 부트캠프에서 했던 로봇 코딩이 생각난다.
'''

'''
import turtle as t
t.shape("turtle")

t.right(60)
t.forward(100)
t.right(120)
t.forward(100)
t.right(120)
t.forward(100)
t.done()
# 와 ... 이건 놀랍다.
# 세모라고 딱 설정된 것은 아니지만 아마 right 1만큼이 1도인것같다. 180도가 평면이니깐
# 60도 만큼 돌고, 100만큼 앞으로 가고, 근데 다음에 60의 2배인 120. 이걸 해줘야 뭔가 연결이 되는 느낌일것같다
'''

'''
import turtle as t
t.shape("turtle")

t.speed(1)
t.forward(100)
t.up()

t.forward(100)
t.down()
t. forward(100)
t.done()
'''

'''
import turtle as t
t.shape("turtle")

t.pensize(3)
t.color("blue")
t.bgcolor("green")
t.fillcolor("yellow")
t.begin_fill()
t.circle(100)
t.end_fill()
t.done()
'''

'''
import turtle as t
t.shape("turtle")

# 5각 별
for a in range(5):
    t.forward(150)
    t.right(144) # 왜 144도 일까?라는 생각이 들긴하지만 어련히 잘 구했으니 별이 나오겠지?

# 24각별?
for a in range(23):
    t.forword(200)
    t.right(172)
t.done()
'''

# 이벤트 처리

'''
import turtle as t

def draw(head,dist): # dist는 또 뭐냐
    t.setheading(head)
    t.forword(dist)

def toleft():
    draw(180,15)

def toright():
    draw(0,15)

def toup():
    draw(90,15)

def todown():
    draw(270,15)

def move(x,y):
    t.up()
    t.setpos(x,y)
    t.down()

t.shape("turtle")
t.speed(0)
t.onkeypress(toleft,"Left")
t.onkeypress(toright,"Right")
t.onkeypress(toup,"Up")
t.onkeypress(todown,"Down")
t.onscreenclick(move)
t.listen()
t.done()

# 졸라 당황스럽네.. 이건 좀 쓰래기 코드 같다
# 나오긴 하지만 작동이 안됨

'''

'''
import turtle as t

def draw(x,y):
    t.setpos(x,y)

def move(x,y):
    t.up()
    t.setpos(x,y)
    t.down()


t.shape("turtle")
t.speed(0)
t.pensize(3)
t.onscreenclick(draw)
t.onscreenclick(move,3)
t.onkeypress(lambda :t.color("red"),"r")
t.onkeypress(lambda :t.color("green"),"g")
t.onkeypress(lambda :t.color("blue"),"b")
t.onkeypress(lambda :t.color("black"),"k")
t.onkeypress(lambda :t.pensize(1),"1")
t.onkeypress(lambda :t.pensize(3),"3")
t.onkeypress(lambda :t.pensize(5),"5")
t.listen()
t.done()

# 이거는 와... 진짜 뭐 이해가 안간다
# 내가 이거를 언제쯤 이해하고 구현하는데 문제가 저언혀 없을까?
# 진짜 궁금하다.
'''
300x250