반응형

 

cx_Oracle의 설치에 이어 DB 연동, 그리고 tkinter를 이용한 GUI 프로그래밍을 해보겠습니다. 화면 구성은 정말 간단히...레이블에 DB 주소를 입력하고 엔터를 누르면 연결해서 결과를 출력하는 구성입니다.

import cx_Oracle 
import os 
import tkinter 

#GUI 부분 

window=tkinter.Tk() 

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

#레이블 
label_ip=tkinter.Label(window, text="예: myid.myweb.com:1526/orcl", width=50, height=2, fg="red", relief="solid") 
label_id=tkinter.Label(window, text="예: erpif", width=20, height=2, fg="red", relief="solid") 
label_pw=tkinter.Label(window, text="예: erp0901", width=20, height=2, fg="red", relief="solid") 



#엔트리 함수 
def set_ip(event): 
    ip_adress=str(eval(entry.get())) 
     
#엔트리 입력창 
entry=tkinter.Entry(window) 
entry.bind("", set_ip) 



#한글지원방법 
os.putenv('NLS_LANG', '.UTF8') 

#연결에 필요한 기본 정보(유저, 비밀번호, 데이터베이스 서버 주소) 
connection = cx_Oracle.connect('login_id','login_pw', ip_adress) 

cursor = connection.cursor() 

cursor.execute(""" 
    select * 
    from TEMP 
    where KORN_NAME='홍길동' 
    """) 

for list in cursor: 
    print(list) 


label_ip.pack() 
label_id.pack() 
label_pw.pack() 

entry.pack(side="top") 

window.mainloop()

 

 

좀 멋도 없었는데, 블러까지 적용하니 참.....한심해보이지만,

그래도 기본적인 내용은 들어있다고 자축하며

이번 예제를 마무리 해봅니다.^^;;;

 

GUI화면에 결과 출력하는 것은 다음기회에...

 

 

반응형
반응형

Python로 Oracle Database에 연결하기 위해서는 cx_oracle 모듈을 설치해야 합니다.

 

설치 방법은 어렵지 않은데 Pycharm과 일반 커맨드창 파이썬에서 실행하는 방법을 나눠 설명하겠습니다.

 

1. 우선 주로 쓰는 Pycharm에서는 File - Settings - Project - Project Interpreter 로 진입합니다. 그리고 cx_Oracle 모듈을 찾아서 설치해 줍니다.

 

cx_Oracle 모듈을 찾아 설치한다.

 

 

 

모듈 설치하고 난 모습

 

2. Command창에 설치해서 사용하는 경우라면,

sudo pip install cx_oracle

로 설치할 수 있습니니다.

 

3. Oracle Client 설치

cx_Oracle을 사용하려면 Oracle Client를 설치해주어야 합니다. Oracle이 설치되어있어도 Library 때문에 필요한 것으로 보입니다. 설치 경로는 여기를 참조하세요.

설치가 다 끝나면 코드 내부에서 해당 라이브러리를 사용하도록 경로를 설정해줍니다. (Path에 추가해주면 된다고 하는데, 잘 안되서 직접 경로를 넣어줬습니다. 이방법이 제일 확실히 되는 것 같습니다.)

cx_Oracle.init_oracle_client(lib_dir=r"D:\8_Programs\Oracle\instantclient_19_9")

 

4. DBconn.py (DB Connection Sample)

import cx_Oracle
import os


#한글지원방법
os.putenv('NLS_LANG', '.UTF8')

# 함수 정의
def connect():
	#라이브러리 연결
    cx_Oracle.init_oracle_client(lib_dir=r"D:\8_Programs\Oracle\instantclient_19_9")
    
    con_ip='localhost:1526/testdb'
    con_id='user'
    con_pw='password'
 
    #연결에 필요한 기본 정보(유저, 비밀번호, 데이터베이스 서버 주소)
    connection = cx_Oracle.connect(con_id,con_pw, con_ip)
    cursor = connection.cursor()
    cursor.execute("""
        select *
        from member
        where name='홍길동'
        """)

    for list in cursor:
        print(list)

    cursor.close()
    connection.close()
    
# 함수 실행    
connect()

 
반응형
반응형

 

 

회사에서는 예전에 만들어둔 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()

 

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

감사합니다.^^

 

 

 

 

 

 

 

 

 

 

반응형

+ Recent posts