Jake Jake -4 years ago 73
Python Question

Python: Calling function from an active class

I've got an active class. That's running the login page.
I click a button to bring up a QWidget, which I've coded to be an onscreen keyboard. And I've got to the part where all I've got left to do with it is pull the current text from a QLineEdit, and then update that text according to what button was pressed on the keyboard. The Keyboard is a seperate class, and I've got a function within my LoginPage that should grab the text from the QLineEdit, but when trying to run that function from another class, I get an error:
TypeError: 'bool' object is not callable

Now I can assume that this error is because the class is active, and in order to call the function within this class from another class, the class has to be called again, which it can't when it's already active. So how would I go about calling this function from another class while the function is in an active class. Or is there another solution.

Yes my keyboard isn't actually part of the current window, it runs seperately so it's universal across the program

SomeCode:(Removed Irrelevant code such as layout and formatting) : Ignore indentation errors as well

class Login(QtGui.QWidget):
clicked =QtCore.pyqtSignal() #Signal emitter
def __init__(self, parent=None): #Constructor
super(Login, self).__init__()
self.initUI()

def initUI(self):
self.EFilter = Filter()

TitleFont = QtGui.QFont()

SubFont = QtGui.QFont()

Title = QtGui.QLabel("SECRET ;)")

LoginL = QtGui.QLabel("Username")

PassL = QtGui.QLabel("Password")

self.LoginE = QtGui.QLineEdit()
self.LoginE.setObjectName("LoginE")
self.LoginE.installEventFilter(self.EFilter)
self.PassE = QtGui.QLineEdit()
self.PassE.setObjectName("PassE")
self.PassE.installEventFilter(self.EFilter)

LoginB = QtGui.QPushButton("Login")

Keyboard = QtGui.QPushButton("Keyboard")
Keyboard.clicked.connect(self.KeyCall)

self.setLayout(NewLayout)

LoginB.clicked.connect(self.LoginRoutine)

def LoginRoutine(self):
User = self.LoginE.text()
Pass = self.PassE.text()
if User == "Admin":
self.parent().parent().AdminSet()
self.LoginE.clear()
self.PassE.clear()

def KeyCall(self):
self.Key = Keyboard(self)

def GiveText(self, widget):
Var = eval("self."+widget+".text()")
return Var




class Keyboard(QtGui.QWidget):
clicked = QtCore.pyqtSignal()
def __init__(self, parent):
super(Keyboard, self).__init__()
self.initUI()

def initUI(self):
#Removed All Buttons, just pointless things not needed to see
self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
self.setFocusPolicy(QtCore.Qt.NoFocus)
self.setFixedSize(750, 250)
self.setWindowTitle("Keyboard")
self.show()

def Capitalise(self):
#This just capitalises buttons nothing relevent here
#self.Capital also comes from here

def Clicked(self):
self.Counter = 0
self.Name = Store(self)
self.Found = False
Login = self.LoginCheck()
if Login == True:
if self.Capital == True:
Char = str(self.sender().text())
#Important Thing: THIS WHERE ERROR IS ;)
OldText = Login().GetText(str(self.Name))
print (OldText) #JUST CHECKING IF IT WORKS WHEN ITS CALLED
#More Irrelevant stuff gone

def LoginCheck(self):
#Boring algorithm stuff to check whether the variable is in this class

def AdminCheck(self):
#Boring algorithm stuff to check whether the variable is in this class

class Filter(QtCore.QObject):
#Typical event filter nothing special

class Store(Filter):
#More boring alogrithm stuff for transfering data between classes


Full Error:

Traceback (most recent call last):
File "Blahblahblah", line 952, in Clicked
OldText = Login().GetText(str(self.Name))
TypeError: 'bool' object is not callable


Any help would be appreciated greatly! Thank you

Notes:
GetAttr method doesn't work either

Answer Source

When you run

Login = self.LoginCheck()

you are setting the name Login to the result of calling self.LoginCheck(), which apparently returns a boolean.

Then later you have

Login()

Since Login now refers to a boolean, this won't work.

Use a different variable name for Login. You shouldn't be using capital letters for variable names anyway.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download