Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to deal with the empty listwidget

I received Excel data and created a program that classifies it into three criteria. However, this program only works when there are files in each of the three list widgets. If there is no file in the listwidget, could you tell me if there is a way to operate it except for that part?

Attached is the program screen below.
enter image description here

Below is the code. Please understand that we only show a part of the code.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

BASE_DIR = os.path.dirname(os.path.abspath(__file__))

form_class_main = uic.loadUiType(BASE_DIR + r"\\AE_main2.ui")[0]
class MainWindow(QMainWindow, QWidget, form_class_main):

def __init__(self):
    super().__init__()
    self.setupUi(self)
    self.btn_open.clicked.connect(self.showDialog)
    self.btn_next.clicked.connect(self.result)
    self.item_right.clicked.connect(self.create_table)

    #데이터 옮기기 버튼 이벤트
    self.left1.clicked.connect(self.clicked_left1_button)
    self.right1.clicked.connect(self.clicked_right1_button)
    self.left2.clicked.connect(self.clicked_left2_button)
    self.right2.clicked.connect(self.clicked_right2_button)
    self.left3.clicked.connect(self.clicked_left3_button)
    self.right3.clicked.connect(self.clicked_right3_button)

def clicked_right1_button(self):
    global path, team_biz
    self.move_current_item(self.listWidget, self.team_business)
    n = self.team_business.count()
    team_biz = [] 
    for a in range(0, n): # qlistWidget 에 올라가있는 현재 파일 목록 출력
        if n == 0:
            return team_biz
        text = self.team_business.item(a).text()
        dirpath = path + r"/{}".format(text)
        team_biz.append(dirpath)

def clicked_left1_button(self):
    self.move_current_item(self.team_business, self.listWidget)

def clicked_right2_button(self):
    global path, team_ath
    self.move_current_item(self.listWidget, self.team_athletic)
    n = self.team_athletic.count()
    team_ath = []
    if n == 0:
        return
    for a in range(0, n):
        if n == 0:
            return team_ath
        text = self.team_athletic.item(a).text()
        dirpath = path + r"/{}".format(text)
        team_ath.append(dirpath)
    print(team_ath)

def clicked_left2_button(self):
    self.move_current_item(self.team_athletic, self.listWidget)

def clicked_right3_button(self):
    global path, team_env
    self.move_current_item(self.listWidget, self.team_environment)
    n = self.team_environment.count()
    team_env = []
    for a in range(0, n): # qlistWidget 에 올라가있는 현재 파일 목록 출력
        if n == 0:
            return team_env
        text = self.team_environment.item(a).text()
        dirpath = path + r"/{}".format(text)
        team_env.append(dirpath)
    print(team_env)

def clicked_left3_button(self):
    self.move_current_item(self.team_environment, self.listWidget)

def move_current_item(self, src, dst):
    if src.currentItem():
        row = src.currentRow()
        dst.addItem(src.takeItem(row))
            
def showDialog(self): # 읽은 파일 이름추출
    files = QFileDialog.getOpenFileNames(self, "파일 선택", '/', "Excel Files(*.xlsx *xls *csv)")
    global path
    fnames = files[0]
    print(type(fnames))
    for i in fnames: # 파일의 경로와 파일명 따로 분리
        s = os.path.basename(f'{i}')
        path = os.path.dirname(f'{i}')
        self.listWidget.addItem(s)

def create_table(self): #받은 엑셀파일 전처리 작업
    pd.options.display.float_format = '{:.5f}'.format
    global AE_cor,team_env, team_biz, team_ath
    AE_cor = pd.DataFrame()
    biz_cor = pd.DataFrame()
    ath_cor = pd.DataFrame()
    env_cor = pd.DataFrame()

    for i in team_biz:
        biz_team = pd.read_excel(f'{i}')
        ......

If there is no file in the listwidget, the following error occurs.

  File "c:\Users\상훈\Desktop\python workspace\.vscode\business_expenses\Agency_expense.py", line 154, in create_table
for i in team_biz:
NameError: name 'team_biz' is not defined

>Solution :

To quickly handle this issue, change:

for i in team_biz:

to:

try:
    team_biz
except NameError:
    team_biz = []
for i in team_biz:

There are many far more elegant ways to handle this issue, but this will get you moving along. Once your code is fully operational, you can improve quick fixes like this one to make your code better.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading