so ive got two scripts thats, where ive imported one into the other. script 1 is a class for the encryption function
class encryption():
def encryption(message):
#code here isnt relevant
def decryption(line_key):
#code isnt relevant
this code works by it self but when the other code from script two tries interacting with it it get the error :
TypeError: encryption() takes no arguments
The code for script two that interacts with script one is :
x1 = entry1.get()
label4 = tk.Label(root, text = encryption.encryption(x1), font = ('helvetica' , 10 , 'bold')
Im really not sure what im doing wrong here.
Oh you have just made a typo.
you should pass self to the definition of encryption and decryption functions in your class.
class encryption():
def encryption(self, message):
#code here isnt relevant
def decryption(self, line_key):
#code isnt relevant
Related
All Python learning that I have been doing shows that to create a new object, all I should have to do is define a variable and tell it what class it is. Most times when I do this, I get an error. What am I missing?
According to just about everything I've been reading or watching on the subject, the code below should work to create a new Railroad object. I have the class as a separate file, which I then include in the main program. The one time I have gotten this to work perfectly is when I created the object as part of a list instead of a stand-alone.
class Railroad:
def __init__(self):
self.name = "Railroad"
self.repmks = "RRRR"
#include Railroad
def create_rr():
rr = Railroad()
rr.name = input("Railroad name: ")
rr.repmks = input("Reporting marks: ")
When this code runs, I get a traceback error telling me "name 'Railroad' is not defined." Did I not already define it as being a class?
You are missing indent.
This part:
class Railroad:
def __init__(self):
self.name = "Railroad"
self.repmks = "RRRR"
needs to be:
class Railroad:
def __init__(self):
self.name = "Railroad"
self.repmks = "RRRR"
You probably need to return the rr variable inside your function.
Here an example:
script1.py
class Railroad:
def __init__(self):
self.name = "Railroad"
self.repmks = "RRRR"
script2.py
from script1 import Railroad
def create_rr():
rr = Railroad()
rr.name = input("Railroad name: ")
rr.repmks = input("Reporting marks: ")
return rr
test = create_rr()
print(test.name)
Thank you to Daniel, Barmar, and Igor - yes, I had mixed up my C++ dabbling with actual Python syntax and didn't import the files correctly. The indent actually wasn't the issue; it was indented in my program but not in the sample code.
Hi I am new in Python and this is an error that many people have but I could'nt be helped by any other thread. The code is straight from this tutorial .
And it posts:[ERROR] behavior.box :FMBox::createPythonModule:0 _Behavior__lastUploadedChoregrapheBehaviorbehavior_1291762048__root__headnod_1: User class evaluation failed with the error:
('expected an indented block', ('', 19, 11, 'motionProxy.angleInterpolation(names,[0.0,1.0],times,True)\n'))
Thank you in advance
class MyClass(GeneratedClass):
def __init__(self):
GeneratedClass.__init__(self)
def onLoad(self):
#put initialization code here
pass
def onUnload(self):
pass
def onInput_onStart(self):
motionProxy=ALProxy("ALMotion")
names = ['HeadYaw','HeadPitch']
times = [[0.5],[0.5]]
motionProxy.angleInterpolation(names,[0.0,0.0],times,True)
for i in range(3):
motionProxy.angleInterpolation(names,[0.0,1.0],times,True)
motionProxy.angleInterpolation(names,[0.0,-1.0],times,True)
motionProxy.angleInterpolation(names,[0.0,0.0],times,True)
self.onStopped()
def onInput_onStop(self):
self.onUnload() #it is recommended to reuse the clean-up as the box is stopped
self.onStopped() #activate the output of the box
Error says that your code is incorrectly indented.
For python you need to indent each block of code, like class body, for loop body, etc:
class MyClass(GeneratedClass):
def __init__(self):
GeneratedClass.__init__(self)
def onLoad(self):
#put initialization code here
pass
def onUnload(self):
pass
def onInput_onStart(self):
motionProxy=ALProxy("ALMotion")
names = ['HeadYaw','HeadPitch']
times = [[0.5],[0.5]]
motionProxy.angleInterpolation(names,[0.0,0.0],times,True)
for i in range(3):
motionProxy.angleInterpolation(names,[0.0,1.0],times,True)
motionProxy.angleInterpolation(names,[0.0,-1.0],times,True)
motionProxy.angleInterpolation(names,[0.0,0.0],times,True)
self.onStopped()
def onInput_onStop(self):
self.onUnload() #it is recommended to reuse the clean-up as the box is stopped
self.onStopped() #activate the output of the box
Check this chapter from Dive Into Python - Indenting Code. Also just a note that code above is not complete, it is a part of something bigger, so you anyway can't just use this code alone to do something.
i'm following a few different guides to re-learn Tkinter by writing a little application that grabs stock prices. My issue that I am up a wall against is calling the .get() method from my entry widget variable. I've seen a couple other suggestions to put all the widget creating inside a function and then call the function in the init method, however I'm getting the same error, even when using the self argument in front of my entry variables. I know it's an issue with the way i'm passing data from function to function, but I can't wrap my head around it. Here's the code, sorry for the wall of text:
class MyApp:
def __init__(self, parent):
self.myParent = parent
self.myContainer1 = Frame(parent)
self.myContainer1.pack()
self.createWidgets()
button1 = Button(self.myContainer1, command = self.button1Click)
button1.configure(text = "get quote")
button1.pack()
def createWidgets(self):
root.title("Stock App")
self.symbol = Entry(self.myContainer1)
self.symbol.pack()
self.symbol.focus_set()
def button1Click(self):
stock = symbol.get()
print stock
I've taken it down to simplest form even and just had the button1Click call a callback function-
def button1Click(self):
print callback()
def callback():
print symbol.get()
This returns the exact same error:
NameError: global name 'symbol' is not defined
Is it getting destroyed too early? how do I fix this?
I've referenced multiple documents for tkinter and have seen some great fixes but none are extensible, or um unable to see how they relate to me using it inside of an object.
Thanks in advance for the help.
As far as I can tell inside of your button1Click method you need to add self as in:
def callback():
print self.symbol.get()
You're missing self. to make the callback:
def callback():
print self.symbol.get()
instead.
I was trying to include my own functions in mainpage class, but when calling them it's not working at all, so what i did is to create a class for it and included that function in it. and in get () of mainpage class i created an instance for that class and called the function like object_name.function name() but it ain't working
class encipher:
def time_stomp():
t1=time.time()
dt = datetime.now()
dt.now()
stri=""
stri+=(str(dt.minute*dt.microsecond)[0:4])
stri+=(str(dt.second*dt.microsecond)[0:2])
stri+=(str(dt.microsecond)[0:3])
stri+=(str(dt.microsecond)[2:3])
stri+=(str(dt.microsecond)[1:2])
stri+=(str(dt.microsecond)[0:1])
return stri
#-------------------------------------------------------------
def keygen():
key_stri=""
ko=0
datalist_str1=self.time_stomp()
for i in range(6):
key_stri+=((hex(operator.xor(int(datalist_str1[ko:ko+2]),128)).replace("0x","")).zfill(2))
ko+=2
#print "Key:",key_stri
#print "Key:",key_stri
#print "Key:",key_stri
return key_stri
class MainPage(webapp.RequestHandler):
def get(self):
ddes=encipher()
global final_data_hex
global username
global filename
username = self.request.get("name")
filename=self.request.get("filename")
addr=self.request.get("mac")
path="d:/xampp/htdocs/encrypt/"+username+'/'+filename
f1 = open(path, 'r')
#f1=open(path,"r")
string=f1.read()
i=0
addr=addr.replace(":",'')
#self.response.out.write(ddes.keygen())
A python instance method needs to accept at least one parameter, self. "It's not working" is a horrible explanation of a problem; if you'd read your tracebacks you'd see an error about .keygen() accepting 0 arguments with 1 provided.
But yes, there's no reason to encapsulate methods in a class if what you really want is a function.
I figured it out. We can simply include functions out of the class and it will work perfectly.
def activate(self,shell):
self.shell = shell
self.action = gtk.Action ('foo','bar','baz',None)
self.activate_id = self.action.connect ('activate', self.call_bk_fn,self.shell)
self.action_group = gtk.ActionGroup ('hot_key_action_group')
self.action_group.add_action_with_accel (self.action, "<control>E")
uim = shell.get_ui_manager ()
uim.insert_action_group (self.action_group, 0)
uim.ensure_update ()
def call_bk_fn(self,shell):
print('hello world')
i am using the above code in a plugin for rhythmbox ,and here i am trying to register the key ctr+e so that the call_bk_fn gets called whenever the key combination is pressed , but its not working why is that so ?
One thing that i did remark is that your callback should be like this:
def call_bk_fn(self, event, shell):
print('hello world')
hope this can help :), if you still have some problem i think you should give us more info about the errors that are raised .