[수경재배기 자동 컨트롤러 제작] 6-4. 설정 폼 만들기

생활/수경재배|2022. 5. 14. 00:39

 

저번에 버튼 배치 했던 버튼 중에

설정 버튼을 클릭시 설정 폼이 나타나서

그 설정 폼에 관련 위젯등을

배치 해 보도록 함.

 

이전 게시글에 이어서 계속 함.

 

import pickle
def stc():
    iotwin.withdraw()
    nw.deiconify()



nw=tk.Tk()
nw.title("수경재배 iot 설정")
nw.geometry('1024x540')
nw.resizable(False, False)
number1=IntVar()
number2=IntVar()
number3=IntVar()
number4=IntVar()
numberw1=IntVar()
numberw2=IntVar()
numberw3=IntVar()
numberw4=IntVar()
# numbero1=IntVar()
# numbero2=Intvar()
# numbero3=IntVar()
# numbero4=IntVar()


h1=[]
m1=[]
n=0
m=0
while n < 24:
    n=n+1
    m=n-1
    h1.append(m)

n=0
m=0
while n < 60:
    n=n+1
    m=n-1
    m1.append(m)

def nwc():
    shoh=lt1toh.current()
    shom=lt1tom.current()
    shfh=lt1tfh.current()
    shfm=lt1tfm.current()

    whoh=wt1toh.current()
    whom=wt1tom.current()
    whfh=wt1tfh.current()
    whfm=wt1tfm.current()

    with open('olgami.set','wb') as file:
        pickle.dump(shoh, file)
        pickle.dump(shom, file)
        pickle.dump(shfh, file)
        pickle.dump(shfm, file)
        pickle.dump(whoh, file)
        pickle.dump(whom, file)
        pickle.dump(whfh,file)
        pickle.dump(whfm,file)
        
    nw.withdraw()
    iotwin.deiconify()

#lt1toh['values']=(h1)
ntime1= int(time.strftime('%H%M'))
lt1t=Label(nw, text="LED 시간 설정",anchor=CENTER,font=('나눔바른펜OTF',35))
lt1t1=Label(nw,text="LED 켜짐",bg="green",anchor=CENTER,font=('나눔바른펜OTF',30))
lt1t2=Label(nw,text="시",anchor=CENTER,font=('나눔바른펜OTF',30))
lt1t3=Label(nw,text="분",anchor=CENTER,font=('나눔바른펜OTF',30))
lt1t4=Label(nw,text="LED 꺼짐",anchor=CENTER,font=('나눔바른펜OTF',30))
lt1t5=Label(nw,text="시",anchor=CENTER,font=('나눔바른펜OTF',30))
lt1t6=Label(nw,text="분",anchor=CENTER,font=('나눔바른펜OTF',30))

wt1t=Label(nw, text="물 펌프시간 설정",anchor=CENTER,font=('나눔바른펜OTF',35))
wt1t1=Label(nw,text="물 펌프 작동",bg="green",anchor=CENTER,font=('나눔바른펜OTF',30))
wt1t2=Label(nw,text="시",anchor=CENTER,font=('나눔바른펜OTF',30))
wt1t3=Label(nw,text="분 마다",anchor=CENTER,font=('나눔바른펜OTF',30))
wt1t4=Label(nw,text="물 펌프 정지",anchor=CENTER,font=('나눔바른펜OTF',30))
wt1t5=Label(nw,text="시",anchor=CENTER,font=('나눔바른펜OTF',30))
wt1t6=Label(nw,text="분 마다",anchor=CENTER,font=('나눔바른펜OTF',30))

lt1toh=ttk.Combobox(nw,values=(h1),font=('나눔바른펜OTF',30),textvariable=number1,width=4,state='readonly')
lt1tom=ttk.Combobox(nw,values=(m1),font=('나눔바른펜OTF',30),textvariable=number2,width=4,state='readonly')
lt1tfh=ttk.Combobox(nw,values=(h1),font=('나눔바른펜OTF',30),textvariable=number3,width=4,state='readonly')
lt1tfm=ttk.Combobox(nw,values=(m1),font=('나눔바른펜OTF',30),textvariable=number4,width=4,state='readonly')

wt1toh=ttk.Combobox(nw,values=(h1),font=('나눔바른펜OTF',30),textvariable=numberw1,width=4,state='readonly')
wt1tom=ttk.Combobox(nw,values=(m1),font=('나눔바른펜OTF',30),textvariable=numberw2,width=4,state='readonly')
wt1tfh=ttk.Combobox(nw,values=(h1),font=('나눔바른펜OTF',30),textvariable=numberw3,width=4,state='readonly')
wt1tfm=ttk.Combobox(nw,values=(m1),font=('나눔바른펜OTF',30),textvariable=numberw4,width=4,state='readonly')

nw.option_add('*TCombobox*Listbox.font',('나눔바른펜OTF',16))

sb=Button(nw,text='저장후 나가기',command=nwc,font=('나눔바른펜OTF',35))

lt1t.place(x=70, y=60, width=280, height=70)
lt1t1.place(x=30, y=150, width=130, height=50)
lt1t2.place(x=210, y=150, width=100, height=50)
lt1t3.place(x=310, y=150, width=130, height=50)
lt1t4.place(x=30, y=250, width=130, height=50)
lt1t5.place(x=210, y=250, width=100, height=50)
lt1t6.place(x=310, y=250, width=130, height=50)
    
lt1toh.place(x=180, y=150, width=60, height=55)
lt1tom.place(x=300, y=150, width=60, height=55)
lt1tfh.place(x=180, y=250, width=60, height=55)
lt1tfm.place(x=300, y=250, width=60, height=55)
    
wt1t.place(x=500, y=60, width=450, height=70)
wt1t1.place(x=450, y=150, width=200, height=50)
wt1t2.place(x=700, y=150, width=100, height=50)
wt1t3.place(x=840, y=150, width=130, height=50)
wt1t4.place(x=450, y=250, width=200, height=50)
wt1t5.place(x=700, y=250, width=100, height=50)
wt1t6.place(x=840, y=250, width=130, height=50)
    
wt1toh.place(x=670, y=150, width=60, height=55)
wt1tom.place(x=790, y=150, width=60, height=55)
wt1tfh.place(x=670, y=250, width=60, height=55)
wt1tfm.place(x=790, y=250, width=60, height=55)
sb.place(x=300, y=330, width=300, height=70)

nw.withdraw()
#nw.mainloop()
#종료
nw.protocol("WM_DELETE_WINDOW",quit2)
iotwin.protocol("WM_DELETE_WINDOW",quit1)

nowtime()
nowtemp()
#read_temp()
dhttemp()

try:
    with open('olgami.set','rb') as file:
        shoh=pickle.load(file)
        shom=pickle.load(file)
        shfh=pickle.load(file)
        shfm=pickle.load(file)
        whoh=pickle.load(file)
        whom=pickle.load(file)
        whfh=pickle.load(file)
        whfm=pickle.load(file)

        lt1toh.current(shoh)
        lt1tom.current(shom)
        lt1tfh.current(shfh)
        lt1tfm.current(shfm)
        wt1toh.current(whoh)
        wt1tom.current(whom)
        wt1tfh.current(whfh)
        wt1tfm.current(whfm)

except:
    lt1toh.current(7)
    lt1tom.current(0)
    lt1tfh.current(21)
    lt1tfm.current(0)

    wt1toh.current(0)
    wt1tom.current(15)
    wt1tfh.current(0)
    wt1tfm.current(45)


iotwin.mainloop()

 

간단하게 설명하자면

전에 버튼 배치할 때, 설정 버튼 클릭시 실행하는 함수를 sfc라고 정했음.

 

def stc():
    iotwin.withdraw()
    nw.deiconify()

설정 버튼을 클릭하면 위의 sfc()함수가 실행함.

iotwin.withdraw()는 iotwin폼을 숨긴다는 의미.

deiconify()는 다시 보이게 한다는 의미.

 

def nwc():
    shoh=lt1toh.current()
    shom=lt1tom.current()
    shfh=lt1tfh.current()
    shfm=lt1tfm.current()

    whoh=wt1toh.current()
    whom=wt1tom.current()
    whfh=wt1tfh.current()
    whfm=wt1tfm.current()

    with open('olgami.set','wb') as file:
        pickle.dump(shoh, file)
        pickle.dump(shom, file)
        pickle.dump(shfh, file)
        pickle.dump(shfm, file)
        pickle.dump(whoh, file)
        pickle.dump(whom, file)
        pickle.dump(whfh,file)
        pickle.dump(whfm,file)

저장후 나가기 버튼을 클릭하면 nwc() 함수가 실행됨.

olgami.set 파일에 쓰기.

 

olgami.set 파일에는 LED 및 워터 펌프 작동 시간 및 꺼짐 시간등의 정보가 담겨져있음.

 

 

만약 olgami.set파일이 없을시

아래 소스가 실행됨.

except:
    lt1toh.current(7)
    lt1tom.current(0)
    lt1tfh.current(21)
    lt1tfm.current(0)

    wt1toh.current(0)
    wt1tom.current(15)
    wt1tfh.current(0)
    wt1tfm.current(45)

즉, olgami.set 파일이 없을시

기본 설정 값으로

LED등이 07시에 켜지고, 21시에 꺼지도록 되어 있으며,

워터펌프는 15분 켜짐, 45분 꺼짐으로 되어 있음

즉 워터펌프가 15분 동안 돌아간 뒤에

45분동안 꺼져있다가, 다시 15분동안 켜지는 걸 반복.

 

이후, 따로 설정 하지 않는 이상

저 기본값으로 계속 유지.

 

lt1toh=ttk.Combobox(nw,values=(h1),font=('나눔바른펜OTF',30),textvariable=number1,width=4,state='readonly')
lt1tom=ttk.Combobox(nw,values=(m1),font=('나눔바른펜OTF',30),textvariable=number2,width=4,state='readonly')
lt1tfh=ttk.Combobox(nw,values=(h1),font=('나눔바른펜OTF',30),textvariable=number3,width=4,state='readonly')
lt1tfm=ttk.Combobox(nw,values=(m1),font=('나눔바른펜OTF',30),textvariable=number4,width=4,state='readonly')

wt1toh=ttk.Combobox(nw,values=(h1),font=('나눔바른펜OTF',30),textvariable=numberw1,width=4,state='readonly')
wt1tom=ttk.Combobox(nw,values=(m1),font=('나눔바른펜OTF',30),textvariable=numberw2,width=4,state='readonly')
wt1tfh=ttk.Combobox(nw,values=(h1),font=('나눔바른펜OTF',30),textvariable=numberw3,width=4,state='readonly')
wt1tfm=ttk.Combobox(nw,values=(m1),font=('나눔바른펜OTF',30),textvariable=numberw4,width=4,state='readonly')

nw.option_add('*TCombobox*Listbox.font',('나눔바른펜OTF',16))

시간 설정은 콤보박스를 이용하여 숫자를 선택하도록 했음


nw.option_add('*TCombobox*Listbox.font',('나눔바른펜OTF',16))

을 사용하여 콤보박스 리스트 글씨폰트랑 일치시킴

 

위에는 그냥 프로그래밍 할 때 대충 한거라 그런데,

왠만하면 콤보박스에 나타나는 폰트체랑 크기랑

콤보박스 리스트에 나타나는 폰트체랑 크기랑

되도록이면 같으면 보기가 좋음.