Search code examples
pythonexceluser-interfacepyqt5qlineedit

PyQt5: How to input default value if QLineEdit is empty?


I have already associated my PyQt5 Gui with Excel.

I would like to input '2000' into Excel via PyQt5 Gui program even if I do not input anything on it.

The explanation photo is below:

enter image description here

even if I do not input anything in PyQt5 Gui,

enter image description here

'2000' should be typed in Excel.

However, the point is if anything input in PyQt5 Gui, it should be written in Excel as it was written on PyQt5.

The code that I made is below:

class Ship_Use_Tug_Input_Program(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        visbl = QLabel('Visibility(m)', self)
        visbl.move(60, 450)
        vis_ent = QLineEdit(self)
        vis_ent.move(180, 445)

        file = pathlib.Path('C:/Users/woody/OneDrive/Desktop/Python Workspace/Ship_Use_Tug_Input_Program.xlsx')
        if file.exists():
            pass
        else:
            file=Workbook()
            sheet = file.active

        file.save('C:/Users/woody/OneDrive/Desktop/Python Workspace/Ship_Use_Tug_Input_Program.xlsx')

    def save_to_excel(self):
        file = openpyxl.load_workbook('C:/Users/woody/OneDrive/Desktop/Python Workspace/Ship_Use_Tug_Input_Program.xlsx')

        sheet = file.active
        sheet.cell(column=10, row=sheet.max_row, value=vis_ent.text())

        file.save('C:/Users/woody/OneDrive/Desktop/Python Workspace/Ship_Use_Tug_Input_Program.xlsx')

    btn_save = QPushButton('S a v e', self)
    btn_save.clicked.connect(save_to_excel)
    btn_save.move(400,510)

    self.setWindowTitle('Ship use tug input program')
    self.setFixedSize(945, 570)
    self.show()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Ship_Use_Tug_Input_Program()
    sys.exit(app.exec_())

Solution

  • You should always add widgets as attributes, so you can access them in other methods:

    class Ship_Use_Tug_Input_Program(QWidget):
        ...    
        def initUI(self):
            self.visbl = QLabel('Visibility(m)', self)
            self.visbl.move(60, 450)
            self.vis_ent = QLineEdit(self)
            self.vis_ent.move(180, 445)
    

    Now you can get the input text:

        def save_to_excel(self):
            ...    
            sheet = file.active
            value = self.vis_ent.text().strip() or '2000'
            sheet.cell(column=10, row=sheet.max_row, value=value)
            ...
    

    If vis_ent is empty or whitespace-only, the default value will be used instead.


    UPDATE:

    Here is what your whole class should look like:

    class Ship_Use_Tug_Input_Program(QWidget):
        def __init__(self):
            super().__init__()
            self.initUI()
    
        def initUI(self):
            self.visbl = QLabel('Visibility(m)', self)
            self.visbl.move(60, 450)
            self.vis_ent = QLineEdit(self)
            self.vis_ent.move(180, 445)
    
            self.btn_save = QPushButton('S a v e', self)
            self.btn_save.clicked.connect(self.save_to_excel)
            self.btn_save.move(400, 510)
    
            self.setWindowTitle('Ship use tug input program')
            self.setFixedSize(945, 570)
            self.show()
    
            file = pathlib.Path('C:/Users/woody/OneDrive/Desktop/Python Workspace/Ship_Use_Tug_Input_Program.xlsx')
            if file.exists():
                pass
            else:
                file=Workbook()
                sheet = file.active
    
            file.save('C:/Users/woody/OneDrive/Desktop/Python Workspace/Ship_Use_Tug_Input_Program.xlsx')
    
        def save_to_excel(self):
            file = openpyxl.load_workbook('C:/Users/woody/OneDrive/Desktop/Python Workspace/Ship_Use_Tug_Input_Program.xlsx')
            sheet = file.active
            value = self.vis_ent.text().strip() or '2000'
            sheet.cell(column=10, row=sheet.max_row, value=value)
    
            file.save('C:/Users/woody/OneDrive/Desktop/Python Workspace/Ship_Use_Tug_Input_Program.xlsx')