โถ ์๋์ฐฝ ๊ตฌ์ฑ
- tkinter ์ ํ์ด์ฌ์์ GUI ๋ชจ๋์ ์ ๊ณตํด์ฃผ๋ ํ์ค ์๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
from tkinter import *
window = Tk()
#Tk()๋ ๊ธฐ๋ณธ์ด ๋๋ ์๋๋ฅผ ๋ฐํํจ
#๋ฃจํธ์๋, ๋ฒ ์ด์ค์๋๋ผ๊ณ ๋ถ๋ฆ / ์คํํ๋ฉด ์๋์ฐฝ์ด ํ๋ฉด์ ๋์ด
window.mainloop()
#10-2
from tkinter import *
window = Tk()
window.title("์๋ ์ฐฝ ์ฐ์ต")
window.geometry("400x100")
window.resizable(width = FALSE, height = FALSE)
window.mainloop()
โถ ๋ ์ด๋ธ
- ๋ฌธ์ ํํํ ์ ์๋ ์์ ฏ
- Label(๋ถ๋ชจ์๋, ์ต์ ... )
from tkinter import *
window = Tk()
photo = PhotoImage(file = "gif/dog.gif")
label1 = Label(window,image = photo) #์ด๋ฏธ์ง๋ฅผ ๊ธ์ ๋์ ์ฌ์ฉ
label1.pack()
window,mainloop()
โถ ๋ฒํผ
- ๋ฒํผ ํด๋ฆญ ํจ๊ณผ์ ์ง์ ํ ์์ ์ด ์คํ๋๋ ์์ ฏ
- Button(๋ถ๋ชจ์๋, ์ต์ ... )
โถํค๋ณด๋์ ๋ง์ฐ์ค ์ด๋ฒคํธ ์ฒ๋ฆฌ
#10-15
from tkinter import*
##ํจ์์ ์ธ๋ถ๋ถ##
def clickMouse(event):
txt=""
if event.num == 1:
txt += "๋ง์ฐ์ค ์ผ์ชฝ ๋ฒํผ์ด ("
elif event.num == 3:
txt +="๋ง์ฐ์ค ์ค๋ฅธ์ชฝ ๋ฒํผ์ด ("
txt += str(event.y) + "," + str(event.x) + ")์์ ํด๋ฆญ๋จ"
label1.configure(text = txt)
##๋ฉ์ธ ์ฝ๋ ๋ถ๋ถ ##
window = Tk()
window.geometry("400x400")
label1 = Label(window, text = "์ด๊ณณ์ด ๋ฐ๋")
window.bind("<Button>",clickMouse)
label1.pack(expand = 1, anchor = CENTER)
window.mainloop()
#10-16
from tkinter import*
from tkinter import messagebox
##ํจ์์ ์ธ๋ถ๋ถ##
def clickMouse(event):
messagebox.showinfo("ํค๋ณด๋์ด๋ฒคํธ","๋๋ฆฐ ํค : "+ chr(event.keycode))
##๋ฉ์ธ ์ฝ๋ ๋ถ๋ถ ##
window = Tk()
window.bind("<Key>",keyEvent)
window.mainloop()
[SELF STUDY 10-4]
from tkinter import*
from tkinter import messagebox
##ํจ์์ ์ธ๋ถ๋ถ##
def keyEvent(event):
if event.keycode == 37:
key = "์ผ์ชฝ"
elif event.keycode == 38:
key = "์์ชฝ"
elif event.keycode == 39:
key = "์ค๋ฅธ์ชฝ"
elif event.keycode == 40:
key = "์๋์ชฝ"
messagebox.showinfo("ํค๋ณด๋์ด๋ฒคํธ","๋๋ฆฐ ํค : " + "shift + " + key + "ํ์ดํ")
##๋ฉ์ธ ์ฝ๋ ๋ถ๋ถ ##
window = Tk()
window.bind( "<Shift-Up>",keyEvent)
window.bind( "<Shift-Down>",keyEvent)
window.bind( "<Shift-Left>",keyEvent)
window.bind( "<Shift-Right>",keyEvent)
window.mainloop()
from tkinter import*
from tkinter.simpledialog import *
##ํจ์์ ์ธ๋ถ๋ถ##
window = Tk()
window.geometry("400x100")
label1=Label(window, text="์
๋ ฅ๋๊ฐ")
label1.pack()
value = askinteger("ํ๋๋ฐฐ์","์ฃผ์ฌ์ ์ซ์ (1~6)์ ์
๋ ฅํ์ธ์",minvalue=1,maxvalue=6)
label1.configure(text=str(value))
window.mainloop()
from tkinter import *
from time import *
## ์ ์ญ ๋ณ์ ์ ์ธ ๋ถ๋ถ ##
fnameList = ["test1.gif", "test2.gif", "test3.gif", "test4.gif", "test5.gif",
"test6.gif", "test7.gif", "test8.gif", "test9.gif",]
photoList = [None] * 9
num = 0
## ํจ์ ์ ์ธ ๋ถ๋ถ ##
def clickNext() :
global num
num += 1
if num > 8 :
num = 0
photo = PhotoImage(file = "chapter10/gif/" + fnameList[num])
pLabel.configure(image = photo)
pLabel.image = photo
def clickPrev() :
global num
num -= 1
if num < 0 :
num = 8
photo = PhotoImage(file = "chapter10/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("<Up>", clickNext) # PageUp key click
window.bind("<Down>", clickPrev) # PageDown key click
photo = PhotoImage(file = "chapter10/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()