如何处理空的listwidget

How to deal with the empty listwidget

我收到了 Excel 数据并创建了一个程序将其分为三个标准。但是,此程序仅在三个列表小部件中的每一个中都有文件时才有效。如果listwidget里面没有文件,请问除了那部分还有没有其他操作方法?

附件是下面的程序屏幕。 enter image description here

下面是代码。请理解我们只展示了部分代码。

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}')
        ......

如果listwidget中没有文件,会出现如下错误。

  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

要快速处理此问题,请更改:

for i in team_biz:

至:

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

有很多更优雅的方法来处理这个问题,但这会让你继续前进。一旦您的代码完全可运行,您就可以像这样改进快速修复以使您的代码更好。