0

我正在编写一个 gui,它有几个用于输入框和下拉菜单的变量。按下“计算按钮”时,我希望它检查是否所有字段都已填写。如果没有,它会弹出一个消息框并列出空的小部件。我不知道该怎么做...

    def next_button_open_results(self):
        if not self.state.varCR.get() or self.state.varTR.get() \
                or self.state.annual_distance_entry.get() or self.state.varStyle or self.state.lifetime_entry.get():
            messagebox.showinfo("Invalid configuration", "Please select:" + ???)
        else:
"CALCULATE"

4

1 回答 1

0

您可以将空的小部件名称保存到数组中,如果数组不为空,则显示消息框:

def next_button_open_results():
  widgets = []
  if self.state.varCR.get().strip() == '':
    widgets.append('varCR')
  if self.state.varTR.get().strip() == '':
    widgets.append('varTR')
  if self.state.varStyle.get().strip() == '':
    widgets.append('varStyle')
  if self.state.annual_distance_entry.get().strip() == '':
    widgets.append('annual_distance_entry')
  if self.state.lifetime_entry.get().strip() == '':
    widgets.append('lifetime_entry')
  if len(widgets) > 0:
    messagebox.showinfo('Invalid configuration', 'Please select: '+', '.join(widgets))
  else:
    # do whatever you want
    pass
于 2020-02-17T05:36:45.883 回答