본문 바로가기
SW 코딩/GUI, tkinter

[파이썬 python] tkinter - 버튼 기능

2021. 8. 25.
반응형

 

 

참고사이트: 유튜브 나도코딩

code - 버튼 기능

2. 버튼기능.txt
0.00MB
img.png
0.00MB

 

code 설명

import tkinter as tk

root = tk.Tk()  # tkinter root창 생성

root.title("계산기") #창 이름

#################################################
'''
창 크기, 가로 x 세로 + 창 출력 위치 좌표
'''
root.geometry("300x500+200+200")

#################################################
'''
기본 버튼 기능 
'''
button1 = tk.Button(root, text="버튼1")
button1.pack()

#################################################
'''
버튼 안에 글자를 먼저 넣은 후에 옆,수직방향으로 공간 확보 
padx : 옆방향으로 공간 확보, pady: 수직방향으로 공간 확보
'''
button2 = tk.Button(root,padx=5, pady=10, text="버튼2")
button2.pack()

#################################################
'''
버튼2번과 비교하면 가로,세로 공간 다름
'''
button3 = tk.Button(root,padx=10, pady=5, text="버튼3")
button3.pack()

#################################################
'''
버튼 안에 글자를 먼저 넣은 후에 가로, 세로 방향으로 공간 확보 
버튼에 넣을 내용이 길어지면 버튼길이도 자동으로 길어진다
padx : 가로 공간 확보, pady: 세로 공간 확보
'''
button4 = tk.Button(root,padx=5, pady=10, text="버튼444444444444444")
button4.pack()


#################################################
'''
width 가로 길이 고정, height 세로 길이 고정
padx,pady와는 다르게 버튼네모 크기를 고정시키는 기능
'''
# width: 창의 x축 값 고정, height: 창의 y축 값 고정
button5 = tk.Button(root, width=10, height=3, text="버튼5")
button5.pack()

#################################################
'''
width 가로 길이 고정, height 세로 길이 고정
padx, pady 기능과는 다르게 text 내용이 길어지면 짤린다
'''
button6 = tk.Button(root,width=10, height=5, text="버튼6666666666666666666")
button6.pack()

#################################################
'''
fg : 글자 색상
bg : 배경색
'''
button7 = tk.Button(root,fg="red", bg="yellow", text="버튼7")
button7.pack()

#################################################
'''
버튼에 사진 넣는 기능
'''
photo=tk.PhotoImage(file="img.png")
button8 = tk.Button(root,image=photo)
button8.pack()

#################################################
'''
버튼클릭시 어떤 행위를 하도록 함
'''
def button_action():
    print("버튼 클릭 완료")

button9 = tk.Button(root, text="실제로 동작 버튼", command=button_action)
button9.pack()

root.mainloop() #mainloop 선언해야 root창이 종료되지 않고 계속 실행됨

 

결과 화면

댓글


loading