반응형
    
    
    
  라벨을 리스트에 계속 담고 한번에 지워줬다.


물건을 치우면 사라진다.

코드는 아래와 같다 .
import tkinter as tk
from numpy import product
#윈도우 생성
root=tk.Tk()
#전체 이름
root.title('무게를 이용한 검수')
#창 크기 +붙은 부분은 좌상단 떨어진 위치
root.geometry("1000x400+100+100")
#창 크기 조절 가능 여부 (디폴트 True)
root.resizable(True,True)
#Fixed Label
lab00=tk.Label(root,text="현재 무게",font=('Arial 32 bold'),bg='black',fg="white",width=8,height=1)
lab00.grid(row=0,column=0,padx=10)
lab10=tk.Label(root,text="회사",font=('Arial 32 bold'),bg='black',fg="white",width=8)
lab10.grid(row=1,column=0,padx=10,pady=20)
lab20=tk.Label(root,text="제품명",font=('Arial 32 bold'),bg='black',fg="white",width=8)
lab20.grid(row=2,column=0,padx=10,pady=20)
#시리얼 데이터 받아오기
import serial
import re
ser = serial.Serial(port='COM8', baudrate=9600, bytesize=serial.EIGHTBITS,
                    parity=serial.PARITY_NONE, timeout=None)
#결과출력
while(True):
    ser.close()
    ser.open()
    #값 불러오기
    res=ser.readline()
    #문자열로 변환
    res=str(res)
    #숫자 부분 추출, 추출 결과는 ['숫자'] 형태
    res=re.findall("\d+.\d+",res)
    #문자열 추출
    if len(res)==0 :
        res="set0"
    else :
        res=res[0]
        #실수로 변환
        res=float(res)
    #현재무게
    lab01=tk.Label(root,text=res,font=('Arial 32 bold'),bg="white",fg="black",width=8)
    lab01.grid(row=0,column=1,padx=10,pady=20) #이렇게 .grid 따로 입력 해야 .destroy() 명령어 적용가능
    #제품 별 무게
    import pandas as pd
    df1=pd.read_csv("경로/ex_weight.csv",encoding='CP949')
    company =''
    product =''
    mycol='black'
    j=1
    #라벨리스트 생성
    labels=[]
    for i in list(df1.index) :
        if df1.loc[i,"최소"] <= res <= df1.loc[i,"최대"] :
            company=df1.loc[i,"회사"]
            product=df1.loc[i,"제품"]
            mycol='red'
            #non-fixed label
            lab11=tk.Label(root,text=company,font=('Arial 32 bold'),bg='white',fg=mycol,width=8)
            lab11.grid(row=1,column=j,padx=10,pady=20)
            lab21=tk.Label(root,text=product,font=('Arial 32 bold'),bg='white',fg=mycol,width=12)
            lab21.grid(row=2,column=j,padx=10,pady=10)
            
            labels.append(lab11) #리스트에 라벨추가
            labels.append(lab21) #리스트에 라벨추가
            
            j=j+1
    root.update() 
    lab01.destroy()
    for i in labels : 
       i.destroy()
root.mainloop()반응형
    
    
    
  'GUI 프로젝트 > 저울 연결하기 (시리얼통신) (Tkinter)' 카테고리의 다른 글
| [파이썬 저울 연결] 19. GUI 만들기 #11. 같은 이름 라벨 지우는 법 찾음 (0) | 2022.01.14 | 
|---|---|
| [파이썬 저울 연결] 18. GUI 만들기 #10. destroy 명령어 오류 해결 (try except 문) (0) | 2022.01.14 | 
| [파이썬 저울 연결] 17. GUI 만들기 #9. 무게 겹칠 경우 다중출력 (0) | 2022.01.14 | 
| [파이썬 저울 연결] 16. GUI 만들기 #8. 코드 개선 (0) | 2022.01.14 | 
| [파이썬 저울 연결] 15. GUI 만들기 #7. 개수 늘려서 속도테스트 (0) | 2022.01.13 | 
 
										
									 
										
									 
										
									 
										
									
댓글