-1

I would like to limit digit input value in PyQt5 QLineEdit Gui in Python.

The explanation photo is below.

enter image description here

Wind direction is from 0 to 359 degree.

so, the value to be able to input is from 0 to 359.

The code that I made is below.

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

    def initUI(self):
        wd = QLabel('Wind Direction', self)
        wd.move(40, 300)
        wd_ent = QLineEdit(self)
        wd_ent.move(180, 295)
        wd_ent.resize(80, 25)
        wd_font = wd.font()
        wd_font.setBold(False)
        wd.setFont(wd_font)
        QToolTip.setFont(QFont('Times New Roman', 8))
        wd.setToolTip('Please type 000 form\nExample)090 degree direction->"Type 090"')

        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/바탕 화면/Python Workspace/Ship_Use_Tug_Input_Program.xlsx')

        def save_to_excel(self):
            file = openpyxl.load_workbook('C:/Users/woody/OneDrive/바탕 화면/Python Workspace/Ship_Use_Tug_Input_Program.xlsx')
            sheet = file.active

            file.save('C:/Users/woody/OneDrive/바탕 화면/Python Workspace/Ship_Use_Tug_Input_Program.xlsx')

        btn_save = QPushButton('Save', self)
        btn_save.clicked.connect(save_to_excel)
        btn_save.move(380,520)

        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_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Woody Kim
  • 29
  • 8
  • I strongly suggest you to take more time in reading the list (and relative documentation) of all [QtWidget classes](https://doc.qt.io/qt-5/qtwidgets-module.html), as this is the second time you ask about extending a feature for which a class already exists. Consider playing around with designer too, which includes almost all basic QtWidgets available. – musicamante Feb 14 '21 at 00:56

1 Answers1

2

In these cases it is better to use a QSpinbox that allows the user to enter integer values within a certain range:

wd_ent = QSpinBox(minimum=0, maximum=359)

Another option is to use a QIntValidator.

Note: Qt offers different types of widgets to obtain different types of information (integers, floats, string, date, time, etc.) so it is recommended that you review the options so you can handle the data in a simple way.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241