본문 바로가기
Programming/Python_Etc

파이썬 tkinter 로 둥근버튼(Round button) 구현하기

by Wilkyway 2021. 9. 26.
반응형

이전 포스팅의 투명한 배경에 이어, 둥근 버튼 구현하기를 알아보겠습니다.

요즘 tkinter와 kivy를 번갈아가며 사용해보고 있는데, 서로 장단점이 존재하네요. 어느것도 완벽한게 없는듯 합니다.

tkinter는 둥근버튼이 없어서 별도로 구현해야 하는 반면 투명한 배경처리가 가능하고,

kivy는 투명한 배경은 안되도 둥근버튼은 쉽게 구현이 되네요. 

import tkinter as tk

root = tk.Tk()
# root.attributes('-alpha', 0.3)  # 앱 전체가 투명해짐
root.wm_attributes("-transparentcolor", "white")

canvas = tk.Canvas(root, width=600, height=300, bg='white')
canvas.grid(columnspan=3, rowspan=3)
hello = tk.Label(root, text="Hello World", font="Raleway", bg='white')
hello.grid(columnspan=3, column=0, row=0)

# 이미지의 사이즈 변경. x방향 1/2, y방향 1/2
btn_img = tk.PhotoImage(file="button.png").subsample(2, 2)
# compound='center'로 이미지의 위치 설정하고 반대편에 텍스트 배치
# (left,right,top,bottom등 가능)
button = tk.Button(root, text="click", image=btn_img, compound='center', borderwidth=0, bg='white')
button.grid(column=1, row=1)

root.mainloop()

 

반응형

댓글