본문 바로가기

IT

파이썬 for beginner 3판 – chapter 10 self study

반응형

chapter 10 - 윈도 프로그래밍


 

selfstudy 10-1


코드

from tkinter import *
window = Tk()
window.title("냥이들 ^^")

photo1 = PhotoImage(file="gif/cat.gif")
label1 = Label(window, image=photo1)

photo2 = PhotoImage(file="gif/cat2.gif")
label2 = Label(window, image=photo2)

label1.pack(side=LEFT)
label2.pack(side=LEFT)

window.mainloop()

 

selfstudy 10-2


코드

from tkinter import *
from random import *

# 변수 선언 부분
btnList = [""] * 9
fnameList = ["honeycomb.gif", "icecream.gif", "jellybean.gif", "kitkat.gif", "lollipop.gif", "marshmallow.gif", "nougat.gif", "oreo.gif", "pie.gif"]
photoList=[None] * 9
i, k=0, 0
xPos, yPos=0, 0
num=0

# 메인 코드 부분
window = Tk()
window.geometry("210x210")

shuffle(fnameList)

for i in range(0,9) :
    photoList[i] = PhotoImage(file="gif/" + fnameList[i])
    btnList[i] = Button(window, image=photoList[i])  

for i in range(0,3) :
    for k in range(0,3) :
        btnList[num].place(x=xPos, y=yPos)
        num += 1
        xPos += 70
    xPos = 0
    yPos += 70

window.mainloop()

 

selfstudy 10-3


코드

from tkinter import *
from time import *

# 변수 선언 부분
fnameList = ["jeju1.gif", "jeju2.gif", "jeju3.gif", "jeju4.gif","jeju5.gif","jeju6.gif","jeju7.gif","jeju8.gif","jeju9.gif"]
photoList=[None] * 9
num=0

# 함수 선언 부분
def clickNext() :
    global num
    num += 1
    if num > 8 :
        num = 0
    photo = PhotoImage(file="gif/" + fnameList[num])
    pLabel.configure(image=photo)
    pLabel.image=photo
    nameLabel.configure(text=fnameList[num])
    
def clickPrev() :
    global num
    num -= 1
    if num < 0 :
        num = 8
    photo = PhotoImage(file="gif/" + fnameList[num])
    pLabel.configure(image=photo)
    pLabel.image=photo
    nameLabel.configure(text=fnameList[num])


# 메인 코드 부분
window = Tk()
window.geometry("700x500")
window.title("사진 앨범 보기")

btnPrev = Button(window, text="<< 이전", command=clickPrev)
btnNext = Button(window, text="다음 >>", command=clickNext)

photo = PhotoImage(file="gif/" + fnameList[0])
pLabel= Label(window, image=photo)

nameLabel = Label(window, text=fnameList[0])

btnPrev.place(x=250, y=10)
nameLabel.place(x=330, y=10)
btnNext.place(x=400, y=10)
pLabel.place(x=15, y=50)

window.mainloop()

 

selfstudy 10-4


코드

from tkinter import *
from tkinter import messagebox

# 함수 정의 부분
def keyEvent(event) :
    txt = "눌린 키 : Shift +  "
    if event.keycode == 37 :
        txt += "왼쪽 화살표"
    elif event.keycode == 38 :
        txt += "위쪽 화살표"
    elif event.keycode == 39 :
        txt += "오른쪽 화살표"
    else :
        txt += "아래쪽 화살표"
        
    messagebox.showinfo("키보드 이벤트",  txt )
   
# 메인 코드 부분
window = Tk()

window.bind("<Shift-Up>",keyEvent)
window.bind("<Shift-Down>",keyEvent)
window.bind("<Shift-Left>",keyEvent)
window.bind("<Shift-Right>",keyEvent)

window.mainloop()

 

selfstudy 10-5


코드

from tkinter import *
from tkinter.filedialog import *
from tkinter.simpledialog import *

## 함수 선언 부분 ##
def func_open() :
    filename = askopenfilename(parent = window, filetypes = (("GIF 파일", "*.gif"), ("모든 파일", "*.*")))
    photo = PhotoImage(file = filename)

    width = photo.width()
    height = photo.height()

    for i in range(height) :
        for k in range(width) :
            r, g, b = photo.get(k, i)
            grey = int((r+g+b)/3)
            photo.put("#%02x%02x%02x" % (grey, grey, grey), (k, i))

    pLabel.configure(image = photo)
    pLabel.image = photo

def func_exit() :
    window.quit()
    window.destroy()


## 메인 코드  부분 ##
window = Tk()
window.geometry("500x500")
window.title("명화 감상하기(흑백)")

photo = PhotoImage()
pLabel = Label(window, image = photo)
pLabel.pack(expand=1, anchor = CENTER)

mainMenu = Menu(window)
window.config(menu = mainMenu)
fileMenu = Menu(mainMenu)
mainMenu.add_cascade(label = "파일", menu = fileMenu)
fileMenu.add_command(label = "파일 열기", command = func_open)
fileMenu.add_separator()
fileMenu.add_command(label = "프로그램 종료", command = func_exit)





window.mainloop()