PyQt5无法正确获取函数和类



Hi需要帮助了解为什么不起作用think是概念性的,但我无法理解。

我有一些文件:

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
from inputgui import Inputgui
from checkstate import check_state
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.QIntValidator()
self.inputguiwin.annualsalaryinput.setValidator(self.validator) 
self.inputguiwin.annualsalaryinput.textChanged.connect(self.check_state)
self.inputguiwin.annualsalaryinput.textChanged.emit(self.inputguiwin.annualsalaryinput.text())
self.inputguiwin.pushButtonOK.clicked.connect(self.qpushButtonOKpressed)
self.inputguiwin.pushButtonCANCEL.clicked.connect(self.pushButtonCANCELpressed)
#    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)        
if __name__ == '__main__':             
app = QtWidgets.QApplication(sys.argv)
window=Menu()
sys.exit(app.exec_())

inputgui.py:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Apr 14 10:40:44 2020
@author: bob
"""
import sys
from PyQt5 import QtWidgets, uic, QtCore
import resource

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)
pollo=Inputgui()
sys.exit(app.exec_())

checkstate.py:

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Sun Apr 19 20:28:32 2020
@author: bob
"""
import sys
from PyQt5 import QtWidgets, uic, QtCore, QtGui
from PyQt5.QtCore import QTimer, Qt

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) 

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>

我从网上复制了这个例子:打开第一个窗口,按钮C,打开新窗口,只能插入整数(QIntValidator(,当尝试插入输入QLineEdit背景更改颜色时。

问题是我无法在外部文件上获取check_state((函数。为了让它工作,我需要在main.py文件中添加它(从checkstate导入check_state添加#To并从main.py上的def check_state中删除####(为什么我无法从外部文件获取函数??

另一方面,为什么QIntValidator阻止我的退格(backcancel(键盘键工作??

您有两个错误:

  • sender((方法属于一个QObject,显然"check_state"不属于任何类,更不属于QObject。

  • "self.check_state"表示类具有check_state,但这显然不是真的。

因此,您将不得不实现不使用sender((而是通过lambda函数传递对象的逻辑(有关更多信息,请参阅此(:

self.inputguiwin.annualsalaryinput.textChanged.connect(lambda text, sender=self.inputguiwin.annualsalaryinput: self.check_state(sender))
def check_state(sender):        
validator = sender.validator()      
state = validator.validate(sender.text(), 0)[0]
# ...

相关内容

  • 没有找到相关文章

最新更新