파이썬 tkinter 버튼 만들기 - paisseon tkinter beoteun mandeulgi

728x90

반응형

마찬가지로 tkinter 를 사용하기 위해 tkinter 모듈안에 모든 것들을 사용하겠다고 정의한다

from tkinter import *

root = Tk()
root.title("Company")

btn1 = Button(root, text="조퇴")
btn1.pack()
btn2 = Button(root, padx=5, pady=10, text="연차")
btn2.pack()
btn3 = Button(root, padx=10, pady=5, text="반차")
btn3.pack()
btn4 = Button(root, width=10, height=3, text="퇴사")
btn4.pack()
btn5 = Button(root, fg="red", bg="yellow", text="존버")
btn5.pack()
photo = PhotoImage(file="gui_basic/img.png")
btn6 = Button(root, image=photo)
btn6.pack()
photo1 = PhotoImage(file="gui_basic/img2.png")
btn7 = Button(root, image=photo1)
btn7.pack()
photo2 = PhotoImage(file="gui_basic/img3.png")
btn8 = Button(root, image=photo2)
btn8.pack()
    
def btncmd():
    print("버튼이 클릭되었어요")

btn9 = Button(root, text="동작하는 버튼", command=btncmd)
btn9.pack()

root.mainloop()

 

그냥 버튼을 한번 만들어보자

from tkinter import *

root = Tk()
root.title("Company")
# 타이틀 이름

btn1 = Button(root, text="버튼")
btn1.pack()

btn9 = Button(root, text="확인", command=btncmd)
btn9.pack()

root.mainloop()

파이썬 tkinter 버튼 만들기 - paisseon tkinter beoteun mandeulgi

 

다음은 버튼을 이용한 위젯을 만들어 보자

from tkinter import *

root = Tk()
root.title("Company")

btn1 = Button(root, text="조퇴")
btn1.pack()
# 버튼의 이름을 지정

btn2 = Button(root, padx=5, pady=10, text="연차")
btn2.pack()
# 버튼의 이름을 지정

btn3 = Button(root, padx=10, pady=5, text="반차")
btn3.pack()
# 버튼의 이름을 지정

btn4 = Button(root, width=10, height=3, text="퇴사")
btn4.pack()
# 버튼의 이름을 지정

btn5 = Button(root, fg="red", bg="yellow", text="존버")
btn5.pack()
# 버튼의 이름을 지정

photo = PhotoImage(file="gui_basic/img.png")
# phto 에 이미지 위치를 변수로 지정한다
btn6 = Button(root, image=photo)
#btn6 에 phto 변수를 불러 온다.
btn6.pack()

photo1 = PhotoImage(file="gui_basic/img2.png")
btn7 = Button(root, image=photo1)
btn7.pack()

photo2 = PhotoImage(file="gui_basic/img3.png")
btn8 = Button(root, image=photo2)
btn8.pack()

    def btncmd():
    print("버튼이 클릭되었어요")
#btn cmd - 버튼을 클릭하게 되었을때 cmd 창에 위 내용을 출력한다

btn9 = Button(root, text="확인", command=btncmd)
btn9.pack()
#확인 버튼 생성


다음과 같이 생성 된 것을 확인 할 수 있다.

 

파이썬 tkinter 버튼 만들기 - paisseon tkinter beoteun mandeulgi

 

 

728x90

반응형

공유하기

게시글 관리

구독하기ONnONs, Future Convergence Institute of Technology

'programming > Tkinter' 카테고리의 다른 글

기본 Frame 만들기  (0)2020.06.22

Button 은 가장 기본적인 GUI 조작 방식이다. 유저가 버튼을 클릭하면, 이벤트가 발생된다. 어느 GUI 나 다 Button 컴포넌트가 있다. widget 이라고 한다. Button 객체의 생성자를 호출한 후 packer로 배치 시킨다. packer는 Tkinter 의 geometry 관리자로 동서남북으로 배치할 수 있다.

 

PACKER의 옵션 사항 링크 https://docs.python.org/3/library/tkinter.html#the-packer

mport tkinter as tk
window = tk.Tk()

button1 = tk.Button(window, text = 'Button 1', fg = 'red')
button1.pack(side = 'left',expand=1)

button2 = tk.Button(window, text = 'Button 2', fg = 'green')
button2.pack(side = 'right',expand=1)

button3 = tk.Button(window, text = 'Button 2', fg = 'blue')
button3.pack(side = 'top',expand=1)

button4 = tk.Button(window, text = 'Button 2', fg = 'orange')
button4.pack(side = 'bottom',expand=1)

window.mainloop()

버튼 생성시 들어가는 인자는 Tk 클래스 참조변수로 지금 열린 창에 표시된다. 버튼의 text 와 fg(색상)을 설정할 수 있다.  packer 호출시 side로 위치를 잡아주고, expand는 윈도우가 확장될 때 상대적 위치로 잡아준다.

 

파이썬 tkinter 버튼 만들기 - paisseon tkinter beoteun mandeulgi
Button packer
파이썬 tkinter 버튼 만들기 - paisseon tkinter beoteun mandeulgi
expeand = 1 시에 확장

* 좀더 추가해보자

import tkinter as tk
window = tk.Tk()

def call1():
    print("Call 1 called")

window.geometry('300x200')

frame = tk.Frame(window)
frame.pack()

button1 = tk.Button(frame, text ="Button 1", command = call1)
button1.pack()

window.mainloop()

 

call1 함수를 정의했다.

 

window.geometry 에서 윈도우창의 크기를 설정한다.

 

그 다음 라인은 프레임을 생성한다. 프레임을 윈도우에 연결한다. 프레임안에 버튼을 담는다. command 에 함수 이름을 넎으면 버튼이 클릭되었을 때 함수를 실행시킨다. 마지막으로 pack으로 frame에 연결한다. pack()을 해야 화면에 나타난다.

파이썬 tkinter 버튼 만들기 - paisseon tkinter beoteun mandeulgi
심플한 GUI
파이썬 tkinter 버튼 만들기 - paisseon tkinter beoteun mandeulgi
버튼으로 함수 호출

* 버튼에 스타일을 추가한다

 

import tkinter as tk
window = tk.Tk()
import tkinter.font as tkFont

def call1():
    print("Call 1 called")

window.geometry('200x200')

frame = tk.Frame(window)
frame.pack()

button1 = tk.Button(frame, text ="Button 1", command = call1,
                    fg = 'blue',font ='Hack', bd = 5, bg ="light green",relief = "groove")
button1.pack(pady = 5)

print(list(tkFont.families()))

window.mainloop()
파이썬 tkinter 버튼 만들기 - paisseon tkinter beoteun mandeulgi
버튼 스타일

 

버튼에 스타일을 추가했다. 상세 옵션이 많으니 튜토리얼을 보고 다양하게 커스터마이즈 해본다.

 

폰트의 이름을 모를 때는 print(list(tkFont.families()))를 사용하면 사용가능한 폰트 리스트를 콘솔에 출력한다.

 

 

버튼 스타일 옵션들 https://coderslegacy.com/python/python-gui/python-tkinter-button/

 

Python Tkinter Button - CodersLegacy

The Python Tkinter Button is a standard Tkinter widget. A button is used as a way for the user to interact with the User interface.

coderslegacy.com

파이썬의 Tkinter는 대형 프로젝트에는 어울리지 않다고 하지만 이 프레임워크를 마스터 하면 다음에 어떤 복잡한 GUI 프레임워크라도 다룰 수 있는 힘을 길러준다. 기본 패키지에 포함되어 있으며 여전히 사용하는 사람수가 가장 많은 GUI 컴포넌트이다.