from tkinter import *
# Tk객체 생성
window =Tk()
window.title("연습하기")
# 기본창 사이즈
window.geometry("400x100")
# 창 크기 조절하기
window.resizable(width=True, height=True)
# 윈도 창 띄우기
window.mainloop()
2. Label을 이용하여 문자 표현하기
from tkinter import *
window = Tk()
label1 = Label(window, text="abcd")
label2 = Label(window, text="efgh", font=("궁서체", 30), fg="blue")
label3 = Label(window, text= "ijkl", bg="magenta", width=20, height=5, anchor=SE)
# anchor를 통해 문자의 위치를 고정
label1.pack()
label2.pack()
label3.pack()
# 해당 레이블을 표시하기
window.mainloop()
from tkinter import *
window = Tk()
btnList = list()
for i in range(10):
btnList.append(Button(window, text="버튼" + str(i+1)))
for btn in btnList:
btn.pack(side=RIGHT)
window.mainloop()
from tkinter import *
window = Tk()
btnList = list()
for i in range(3):
btnList.append(Button(window, text="버튼" + str(i+1)))
for btn in btnList:
btn.pack(side=TOP, fill=X)
# fill 인자에 X를 줌으로써 해당 창을 채운다.
window.mainloop()
9. padding과 margin 주기
from tkinter import *
window = Tk()
btnList = list()
for i in range(3):
btnList.append(Button(window, text="버튼" + str(i+1)))
for btn in btnList:
btn.pack(side=TOP, fill=X, ipadx=10, ipady=10)
window.mainloop()
from tkinter import *
window = Tk()
btnList = list()
for i in range(3):
btnList.append(Button(window, text="버튼" + str(i+1)))
for btn in btnList:
btn.pack(side=TOP, fill=X, padx=10, pady=10)
window.mainloop()
10. 위젯을 고정위치에 배치하기
from tkinter import *
window = Tk()
btnList = list()
for i in range(9):
btnList.append(Button(window, text="버튼" + str(i+1)))
x = 0
y = 0
for i in range(3):
x = 0
for j in range(3):
btnList[i*3+j].place(x=x, y=y, width=50, height=30)
x += 50
y += 30
window.mainloop()
from tkinter import *
import random
SIZE = 80
photos = ['eclair', 'froyo', 'gingerbread', 'honeycomb',
'jellybean', 'kitkat', 'lollipop', 'marshmallow', 'nougat']
random.shuffle(photos)
window = Tk()
window.geometry("{}x{}".format(SIZE*3, SIZE*3))
x = 0
y = 0
photoList = [None for _ in range(9)]
for i in range(9):
photoList[i] = PhotoImage(file='gif/' + photos[i] + '.gif')
for i in range(3):
x = 0
for j in range(3):
btn = Button(window, image=photoList[i*3 + j])
btn.place(x=x, y=y)
x += SIZE
y += SIZE
window.mainloop()