Привет, я использую Python и PyQt5,
, получая данные от QLineEdit. Я проверяю его через QDoubleValidator. Я заметил, что QDoubleValidator принимает запятую и точку в качестве десятичного разделителя только для значений 0,1 и 0,1. (используя вход в функции с 0,1, я получу ошибки в простой базовой операции c, не описанной здесь).
Запустите main.py Нажмите кнопку C Открывает новое окно QLineEdit окно должно быть зеленый для допустимых вводов от 0,1 до 1,0 QtGui.QDoubleValidator(0.100, 1.00, 2)
по какой-то причине он становится зеленым с 0,1 тоже
main.py:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 8 14:25:12 2020
@author: Pietro
"""
import sys
from PyQt5 import QtWidgets, uic, QtCore, QtGui
from PyQt5.QtCore import QTimer, Qt
class Menu(QtWidgets.QMainWindow):
def __init__(self):
super(Menu, self).__init__()
uic.loadUi('main2.ui', self)
self.setFixedSize(544,686)
self.show()
self.ButtonC.clicked.connect(self.inputs)
def inputs(self):
self.hide()
self.inputguiwin=Inputgui()
self.inputguiwin.show()
self.validator = QtGui.QDoubleValidator(0.100, 1.00, 2)
self.inputguiwin.annualsalaryinput.setValidator(self.validator)
self.inputguiwin.annualsalaryinput.textChanged.connect(self.check_state)
self.inputguiwin.annualsalaryinput.textChanged.emit(self.inputguiwin.annualsalaryinput.text())
def check_state(self):
sender = self.sender()
validator = sender.validator()
state = validator.validate(sender.text(), 0)[0]
if state == QtGui.QIntValidator.Acceptable:
color = 'green'
elif state == QtGui.QIntValidator.Intermediate:
color = 'yellow'
else:
color = 'red'
sender.setStyleSheet('QLineEdit { background-color: %s }' % color)
class Inputgui(QtWidgets.QMainWindow):
def __init__(self):
super(Inputgui, self).__init__()
uic.loadUi('inputgui2.ui', self)
self.setFixedSize(904,661)
print('inside inputgui ' *5)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window=Menu()
sys.exit(app.exec_())
main2.ui:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="windowModality">
<enum>Qt::WindowModal</enum>
</property>
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>487</width>
<height>415</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<property name="windowIcon">
<iconset>
<normaloff>:/main/python.png</normaloff>
<normalon>:/main/python.png</normalon>
<disabledoff>:/main/python.png</disabledoff>
<disabledon>:/main/python.png</disabledon>
<activeoff>:/main/python.png</activeoff>
<activeon>:/main/python.png</activeon>
<selectedoff>:/main/python.png</selectedoff>
<selectedon>:/main/python.png</selectedon>:/main/python.png</iconset>
</property>
<property name="styleSheet">
<string notr="true"/>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QPushButton" name="ButtonC">
<property name="geometry">
<rect>
<x>150</x>
<y>110</y>
<width>151</width>
<height>81</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>20</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>C</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>487</width>
<height>29</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
inputgui2.ui:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="windowModality">
<enum>Qt::WindowModal</enum>
</property>
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>422</width>
<height>276</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<property name="layoutDirection">
<enum>Qt::LeftToRight</enum>
</property>
<property name="autoFillBackground">
<bool>true</bool>
</property>
<property name="styleSheet">
<string notr="true">QPushButton#pushButtonOK{
background-color: #9de650;
}
QPushButton:hover#pushButtonOK{
background-color: green;
}
QPushButton#pushButtonCANCEL{
background-color: orange;
}
QPushButton:hover#pushButtonCANCEL{
background-color: red;
}
</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QLineEdit" name="annualsalaryinput">
<property name="geometry">
<rect>
<x>80</x>
<y>80</y>
<width>131</width>
<height>51</height>
</rect>
</property>
<property name="contextMenuPolicy">
<enum>Qt::DefaultContextMenu</enum>
</property>
<property name="layoutDirection">
<enum>Qt::RightToLeft</enum>
</property>
<property name="text">
<string> Annual Salary</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
<property name="clearButtonEnabled">
<bool>true</bool>
</property>
</widget>
<widget class="QLabel" name="annualsalarylabel">
<property name="geometry">
<rect>
<x>230</x>
<y>90</y>
<width>151</width>
<height>41</height>
</rect>
</property>
<property name="text">
<string> Annual Salary</string>
</property>
</widget>
<widget class="QLabel" name="titlelabel">
<property name="geometry">
<rect>
<x>210</x>
<y>0</y>
<width>471</width>
<height>71</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>16</pointsize>
<weight>75</weight>
<bold>true</bold>
<underline>true</underline>
</font>
</property>
<property name="styleSheet">
<string notr="true"/>
</property>
<property name="text">
<string/>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>422</width>
<height>29</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>