Why can't i generalize this function? [duplicate] - python

This question already has answers here:
PyQt TypeError connect()
(1 answer)
Passing extra arguments through connect
(1 answer)
Closed 1 year ago.
I created a calculator using PyQt5 designer, in the logic part written in Python each button call a class to add the corresponding symbol like this:
self.button_0.clicked.connect(self.push0)
self.button_1.clicked.connect(self.push1)
def push0(self):
t=self.display.toPlainText()
self.display.setText(t+"0")
def push1(self):
t=self.display.toPlainText()
self.display.setText(t+"1")
But if i try to write one function for all of them like:
self.button_0.clicked.connect(self.push(self, '0'))
self.button_0.clicked.connect(self.push(self, '1'))
def push(self, c):
t=self.display.toPlainText()
self.display.setText(t+c)
I get the message "TypeError: push() takes 2 positional arguments but 3 were given"
What's the problem?
Thans to everyone already

Related

insert argument to map function python [duplicate]

This question already has answers here:
Map list item to function with arguments
(4 answers)
Closed 1 year ago.
I have a following function:
def calculate_frequent_itemset(fractional_data, support):
"""Function that calculated the frequent dataset parallely"""
return apriori(fractional_data, min_support=support, use_colnames=True)
I would like to call it in a map function using:
frequent_itemsets=p.map(calculate_frequent_itemset,(dataNew1,dataNew2,dataNew3,dataNew4,dataNew5), 200)
In other words I want to set 200 as support argument. But I got an error TypeError: calculate_frequent_itemset() missing 1 required positional argument: 'support'. How can I fix it please?
You could use a wrapper function (or a lambda):
support=200
def my_calculate_frequent_itemset(fractional_data):
return calculate_frequent_itemset(fractional_data, support)
frequent_itemsets=p.map(my_calculate_frequent_itemset,(dataNew1,dataNew2,dataNew3,dataNew4,dataNew5))
It's pretty horrible.
I rediscovered this - rather perhaps see https://stackoverflow.com/a/31995624/1021819 which gives the same answer

Is there a way to call a function through a variable? [duplicate]

This question already has answers here:
Calling a function of a module by using its name (a string)
(18 answers)
Closed 2 years ago.
I'm really new to Python but I'm trying to do something definitely above my skill level.
Is it possible to calla a function though a variable? For example,
def one():
print("this is a function")
progress = "one"
progress()
with the intention of calling the function "one()".
Is this possible?
Just take off the double quotes around "one" and you should be good.
def one():
print("this is a function")
progress = one
progress()

How can I pass the name of `ObjectName` like `self.lineEdit_260` as a parameter in a method using python 3.4? [duplicate]

This question already has answers here:
Argument 1 has unexpected type 'NoneType'?
(2 answers)
Closed 3 years ago.
How can I pass the name of ObjectName like self.lineEdit_260 as a parameter in a method using python 3.4?
self.Open_Fan_J104_A_stage_2(no=self.lineEdit_260.text()))
def Open_Fan_J104_A_stage_2(self,no):
if no=='' and self.lineEdit_247.text()=='':
print(type(self.lineEdit_260.text()))
self.start_time=datetime.datetime.now()
self.lineEdit_260.setText(str(self.start_time))
elif self.lineEdit_247.text()=='':
QMessageBox.information(self, 'x')
else:
self.lineEdit_260.setText('')
self.start_time = datetime.datetime.now()
self.lineEdit_260.setText(str(self.start_time))
self.lineEdit_247.setText('')
self.lineEdit_241.setText('')
When I run this code:
self.pushButton_123.clicked.connect( self.Open_Fan_J104_A_stage_2(no=self.lineEdit_260.text()))
TypeError: argument 1 has unexpected type 'NoneType'
I think, the Open_Fan_J104_A_stage_2 expects a callable and since there was a lack of information. So try,
self.Open_Fan_J104_A_stage_2(lambda: self.lineEdit_260.text()))

How to get the result of a function activated through a tk.Button? [duplicate]

This question already has answers here:
Python Tkinter Return
(2 answers)
Closed 8 months ago.
I'm currently working on a GUI project on Python (3.6) using tkinter (8.6).
Following this question I'm wondering how to get back the result of someFunction :
def someFunction(event):
do stuff ..
return(otherStuff)
canvas.bind('<Button-1>',lambda event: someFunction(event))
Thank you in advance :) !
The return values of callback functions like your someFunction are ignored. Rather than using return, have the callback save the value somewhere (in a global variable or an attribute of some kind of object). Or have your function pass the computed value as an argument to some other function that will do something with it.

__init__(self, grammar, **options) and I pass (grammar_string,myoptdict): TypeError: 2 args, 3 given [duplicate]

This question already has answers here:
What is the purpose and use of **kwargs? [duplicate]
(13 answers)
Closed 6 years ago.
I am using plyplus and just trying to get the debug option turned on.
The Grammar class is defined like:
class Grammar(object):
def __init__(self, grammar, **options)
and I am invoking it via
options = { 'debug' : True }
Grammar(long_string, options)
but I get this error:
TypeError: __init__() takes exactly 2 argument (3 given)
I am going mad looking at this; what's wrong? This is using Python 2.7 on Ubuntu and I verified that there is no older version of plyplus on the system with one fewer argument.
Zondo's answer worked for me:
Grammar(mylongstring, **options)
I've been using Python for some years but never came across this before.
Thanks!

Categories

Resources