디필의 요모조모
파이썬 for Beginner 10장 연습문제 본문
1. 300 x 300 크기의 윈도창을 만들고, 윈도창의 크기가 변경되지 않도록 하는 코드이다. 빈칸에 들어갈 내용을 보기에서 골라 넣으시오.
resizable, geometry, mainloop
from tkinter import *
window = Tk()
window.title("윈도창 연습")
window.geometry("400x100")
window.resizable(width = FALSE, height = FALSE)
window.mainloop()
2. 버튼을 클릭하면 func1() 함수가 호출되는 코드이다. 빈칸을 채우시오.
myBtn = Button(window, text = "파이썬 종료", fg = "red", command = func1)
3. 버튼 5개를 수직으로 정렬하는 코드이다. 빈칸을 채우시오.
from tkinter import *
window = Tk()
btnList = [None] * 5
for i in range(0, 5):
btnList[i] = Button(window, text = "버튼" + str(i+1))
for btn in btnList:
btn.pack(sid = TOP)
window.mainloop()
4. 윈도창에서 마우스 오른쪽 버튼을 더블클릭하면 실행되는 코드이다. 빈칸을 채우시오.
from tkinter import *
from tkinter import messagebox
def myClick(event):
messagebox.showinfo("마우스", "마우스 오른쪽 버튼이 더블클릭됨.")
window = Tk()
window.bind("<Double-Button-3>", myClick)
window.mainloop()
5. [프로그램 1]을 수정해서 PageUp이나 PageDown을 눌러도 사진이 바뀌는 기능을 추가하시오. 결과 화면은 [프로그램 1]과 동일하다.
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 = "cookpython\gif/" + fnameList[num])
pLabel.configure(image = photo)
pLabel.image = photo
def clickPrev() :
global num
num -= 1
if num < 0 :
num = 8
photo = PhotoImage(file = "cookpython\gif/" + fnameList[num])
pLabel.configure(image = photo)
pLabel.image=photo
def keyEvent(event) :
global num
if event.keycode == 33:
num += 1
if num > 8:
num = 0
photo = PhotoImage(file = "cookpython\gif/" + fnameList[num])
pLabel.configure(image = photo)
pLabel.image = photo
elif event.keycode == 34:
num -= 1
if num < 0:
num = 8
photo = PhotoImage(file = "cookpython\gif/" + fnameList[num])
pLabel.configure(image = photo)
pLabel.image = photo
window = Tk()
window.geometry("700x500")
window.title("사진 앨범 보기")
btnPrev = Button(window, text = "<< 이전", command = clickPrev)
btnNext = Button(window, text = "다음 >>", command = clickNext)
window.bind("<Key>", keyEvent)
photo = PhotoImage(file = "cookpython\gif/" + fnameList[0])
pLabel = Label(window, image = photo)
btnPrev.place(x = 250, y = 10)
btnNext.place(x = 400, y = 10)
pLabel.place(x = 15, y = 50)
window.mainloop()
6. [File] 메뉴 아래에 [Open], [Save] 메뉴 2개를 추가하는 코드이다. 빈칸에 들어갈 내용을 보기에서 골라 넣으시오.
add_cascade, add_command, Menu
fileMenu = Menu(mainMenu)
mainMenu.add_cascade(label = "File", menu = fileMenu)
fileMenu.add_command(label = "Open")
fileMenu.add_command(label = "Save")
7. askinteger(), askstring(), askfloat() 함수를 사용할 때 임포트해야 하는 모듈은?
① tkinter
② tkinter.simpledialog
③ tkinter.dialog
④ tkinter.filedialog
8. askinteger() 함수에서 입력값의 범위를 제한할 때 사용하는 항목은?
① min, max
② mindata, maxdata
③ minimum, maximum
④ minvalue, maxvalue
9. [파일 열기] 대화상자 및 [다른 이름으로 저장] 대화상자를 나타내는 코드이다. 빈칸을 채우시오.
askopenfilename(parent = window, filetypes = (("GIF 파일", "*.gif"), ("모든 파일", "*.*")))
asksaveasfile(parent = window, mode = "w", defaultextension = ".jpg", filetypes = (("JPG 파일", "*.jpg;*.jpeg"), ("모든 파일", "*.*")))
10. (심화문제) [프로그램 2]에 상위 메뉴 [이미지 효과]와 하위 메뉴 [확대하기], [축소하기]를 추가하시오. 그리고 [확대하기] 메뉴를 선택하면 이미지가 확대되고, [축소하기] 메뉴를 선택하면 이미지가 축소되도록 코드를 추가하시오.
from tkinter import *
from tkinter.filedialog import *
from tkinter.simpledialog import *
def func_open():
global filename
filename = askopenfilename(parent = window, filetypes = (("GIF 파일", "*.gif"),
("모든 파일", "*.*")))
photo = PhotoImage(file = filename)
pLabel.configure(image = photo)
pLabel.image = photo
def func_exit():
window.quit()
window.destroy()
def func_zoom():
value = askinteger("확대배수","확대할 배수를 선택하세요(1~10)",minvalue = 1,maxvalue = 10)
photo = PhotoImage(file = filename)
photo = photo.zoom(value,value)
pLabel.configure(image = photo)
pLabel.image = photo
def func_subsample():
value = askinteger("축소배수","축소할 배수를 선택하세요(1~10)",minvalue = 1,maxvalue = 10)
photo = PhotoImage(file = filename)
photo = photo.subsample(value,value)
pLabel.configure(image = photo)
pLabel.image = photo
window = Tk()
window.geometry("400x400")
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)
imageMenu = Menu(mainMenu)
mainMenu.add_cascade(label = '이미지 효과', menu = imageMenu)
imageMenu.add_command(label = '확대하기', command = func_zoom)
imageMenu.add_separator()
imageMenu.add_command(label = '축소하기', command = func_subsample)
window.mainloop()
'Programming Language > Python' 카테고리의 다른 글
파이썬 for Beginner 12장 연습문제 (0) | 2019.12.08 |
---|---|
파이썬 for Beginner 11장 연습문제 (0) | 2019.12.08 |
파이썬 for Beginner 9장 연습문제 (0) | 2019.12.08 |
파이썬 for Beginner 8장 연습문제 (0) | 2019.12.08 |
파이썬 for Beginner 7장 연습문제 (0) | 2019.12.08 |