본문 바로가기
Programming/Python_Etc

Python GUI - tkinter 예제

by Wilkyway 2020. 2. 25.
반응형

 

 

회사에서는 예전에 만들어둔 Visual Basic 6.0을 어쩌지 못해 계속 사용하고 있습니다. 저는 그 유지보수 담당입니다. 다른 언어로 갈아 엎고는 싶지만,,,  온지 얼마 안돼서..아니, 실력이 아직 한참 모자라서 그냥 유지보수 중입니다.

그러나 마음만은 항상 다른 언어로 포팅하려고 준비하고 있습니다. 배포가 편리한 Java를 유력 후보로 생각하고 있는데요..(C# 안사줌..ㅠㅠ)

그 전에 간단한 프로그램 프로토타입 설계를 위해 tkinter를 손대보기로 했습니다.

 

결론은, 필요한 기능 그때그때 불러다 쓸 기본 위젯 종합 선물세트를 만들었습니다.  필요할 때 골라 쓰려구요. 실행하면 이런 gui프로그램이 나오게 될 것입니다.

 

그리고, 소스는 https://076923.github.io/ 사이트를 참조했음을 알려드리며, 개발자분께 감사드립니다. 자세한 모듈 속성에 대한 설명도 해당 사이트에서 확인할 수 있습니다.


import tkinter

window=tkinter.Tk()

#기본 설정
window.title("GUI Sample")
window.geometry("640x640+100+100")  #너비X높이+X좌표+Y좌표
window.resizable(True, True)        #사이즈 변경 가능

#레이블
label_1=tkinter.Label(window, text="위젯 테스트용입니다.", width=20, height=5, fg="red", relief="solid")
label_2=tkinter.Label(window, text="", width=20, height=3, fg="red", relief="solid")

label_1.pack()
label_2.pack()

#버튼
button = tkinter.Button(window, text="hi", overrelief="solid", width=20)
button.pack()


# 엔트리 함수
def calc(event):
    label.config(text="결과=" + str(eval(entry.get())))

#엔트리 입력창
entry=tkinter.Entry(window)
entry.bind("<Return>", calc)
entry.pack()

#리스트박스
listbox=tkinter.Listbox(window, selectmode='extended', height=0)
listbox.insert(0, "no1")
listbox.insert(1, "no2")
listbox.insert(2, "no3")
listbox.insert(3, "no4")
listbox.insert(4, "no5")
listbox.pack()

#체크박스 함수
def flash():
    button.flash()

#체크박스
CheckVariety_1=tkinter.IntVar()
CheckVariety_2=tkinter.IntVar()

checkbutton1=tkinter.Checkbutton(window,text="O", variable=CheckVariety_1, activebackground="blue")
checkbutton2=tkinter.Checkbutton(window, text="Y", variable=CheckVariety_2)
checkbutton3=tkinter.Checkbutton(window, text="X", variable=CheckVariety_2, command=flash)

checkbutton1.pack()
checkbutton2.pack()
checkbutton3.pack()

#라디오버튼
RadioVariety_1=tkinter.IntVar()

#value값이 같을 경우 함께 선택됨
radio1=tkinter.Radiobutton(window, text="1번", value=3, variable=RadioVariety_1)
radio1.pack()
radio2=tkinter.Radiobutton(window, text="2번", value=3, variable=RadioVariety_1)
radio2.pack()
radio3=tkinter.Radiobutton(window, text="3번", value=9, variable=RadioVariety_1)
radio3.pack()

#메뉴
def close():
    window.quit()
    window.destroy()

menubar=tkinter.Menu(window)

menu_1=tkinter.Menu(menubar, tearoff=0)
menu_1.add_command(label="Sub Menu1-1")
menu_1.add_command(label="Sub Menu1-2")
menu_1.add_separator()
menu_1.add_command(label="종료", command=close)
menubar.add_cascade(label="Menu1", menu=menu_1)

menu_2=tkinter.Menu(menubar, tearoff=0, selectcolor="red")
menu_2.add_radiobutton(label="Sub Menu2-1", state="disable")
menu_2.add_radiobutton(label="Sub Menu2-2")
menu_2.add_radiobutton(label="Sub Menu2-3")
menubar.add_cascade(label="Menu2", menu=menu_2)

menu_3=tkinter.Menu(menubar, tearoff=0)
menu_3.add_checkbutton(label="Sub Menu3-1")
menu_3.add_checkbutton(label="Sub Menu3-2")
menu_3.add_checkbutton(label="Sub Menu3-3")
menubar.add_cascade(label="Menu3", menu=menu_3)


#메뉴 버튼
menubutton=tkinter.Menubutton(window,text="Menu Button", relief="raised", direction="right")
menubutton.pack()

menu=tkinter.Menu(menubutton, tearoff=0)
menu.add_command(label="Sub-1")
menu.add_separator()
menu.add_command(label="Sub-2")
menu.add_command(label="Sub-3")

menubutton["menu"]=menu
window.config(menu=menubar)

#배치(place(), pack(), grid())
b1=tkinter.Button(window, text="top")
b1.pack(side="top")
b2=tkinter.Button(window, text="bottom")
b2.pack(side="bottom")
b3=tkinter.Button(window, text="left")
b3.pack(side="left")
b4=tkinter.Button(window, text="right")
b4.pack(side="right")

bb1=tkinter.Button(window, text="(50, 50)")
bb2=tkinter.Button(window, text="(50, 100)")
bb3=tkinter.Button(window, text="(100, 150)")
bb4=tkinter.Button(window, text="(0, 200)")
bb5=tkinter.Button(window, text="(0, 300)")
bb6=tkinter.Button(window, text="(0, 300)")

bb1.place(x=50, y=50)
bb2.place(x=50, y=100, width=50, height=50)
bb3.place(x=100, y=150, bordermode="inside")
bb4.place(x=0, y=200, relwidth=0.5)
bb5.place(x=0, y=300, relx=0.5)
bb6.place(x=0, y=300, relx=0.5, anchor="s")

"""     grid() 는 pack()과 함께 쓰일 수 없음

bbb1=tkinter.Button(window, text="(0,0)")
bbb1.grid(row=0, column=0)
bbb2=tkinter.Button(window, text="(1,1)", width=20)
bbb2.grid(row=1, column=1, columnspan=3)
bbb3=tkinter.Button(window, text="(1,2)")
bbb3.grid(row=1, column=2)
bbb4=tkinter.Button(window, text="(2,3)")
bbb4.grid(row=2, column=3)
"""

window.mainloop()

 

필요하신 분, 가져다가 적극 활용하시기 바랍니다.

감사합니다.^^

 

 

 

 

 

 

 

 

 

 

반응형

댓글